Attaching .txt files in .NET
I am developing a desktop application in VB.NET 2005. I have a requirement whereby a user need to 开发者_StackOverflow中文版select a .txt file from his machine (client) and after clicking the save button, i need this file to be saved in the server into a specific folder.
Similarly, i should be able to retrieve the file as well.
Please help on this requirement.
This is the way I do it. The things you need is a folder named "Uploads". All the rest is done automaticaly.
First when a user open's the page where the uploading is done on a Page_Load I do the following to make sure the folder for the authenticated user is created.
Protected Sub Page_Load (ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If My.User.IsAuthenticated Then
Try
If Not Page.IsPostBack Then
Select Case My.Computer.FileSystem.DirectoryExists (Server.MapPath ("~/Uploads"))
Case False
My.Computer.FileSystem.CreateDirectory (Server.MapPath ("~/Uploads"))
End Select
Select Case My.Computer.FileSystem.DirectoryExists (Server.MapPath ("~/Uploads/" & My.User.Name))
Case False
My.Computer.FileSystem.CreateDirectory (Server.MapPath ("~/Uploads/" & My.User.Name))
End Select
ListMyFiles()
End If
Catch ex As Exception
'Some code for catching exceptions
End Try
Else
Response.Redirect ("YOUR LOGIN PAGE")
End If
End Sub
Then on the page I already added a ASP:FileUpload control. On the Click eveent of this control I add this code
Try
Select Case FileUpload1.HasFile
Case True
Dim fname = FileUpload1.PostedFile.FileName
SaveFile(FileUpload1.PostedFile)
ListMyFiles()
Case False
Fupload.Text = "Please select a file for uploading!"
End Select
Catch ex As Exception
'Some code for catching exceptions
End Try
The SaveFile function has the following code
Sub SaveFile (ByVal file As HttpPostedFile)
Try
Dim _
filext = _
Split (FileUpload1.PostedFile.FileName, ".") ( _
Split (FileUpload1.PostedFile.FileName, ".").Length - _
1)
Select Case filext
Case "txt"
Dim foldername = "~/Uploads/" & My.User.Name
Dim filename = foldername & "/Uploaded_" & FileUpload1.FileName
Dim savePath As String = "~/Uploads/" & My.User.Name
Dim pathToSave As String = Server.MapPath (filename)
Select Case IO.File.Exists (pathToSave)
Case False
FileUpload1.SaveAs (pathToSave)
Dim uploadedFile = My.Computer.FileSystem.ReadAllText ((pathToSave))
uploadedFile = uploadedFile
My.Computer.FileSystem.WriteAllText (pathToSave, uploadedFile, False)
Dim msg As String = "Your file was uploaded successfully."
Fupload.Text = msg
Case True
Dim _
msg As String = _
"You have already uploaded this file. Please delete the file from server first and then try to upload it again."
Fupload.Text = msg
End Select
Case Else
Dim _
msg As String = "The file type '" & filext & _
"' you are trying to upload is not allowed. You can only upload '.txt' files."
Fupload.Text = msg
End Select
Catch ex As Exception
'Some code for catching exceptions
End Try
End Sub
and the ListMyFiles functions has the following code.
Protected Sub ListMyFiles()
Try
Dim foldername = "~/Uploads/" & My.User.Name
Dim files As New List(Of MyFiles)
For Each s As String In My.Computer.FileSystem.GetFiles (Server.MapPath (foldername) & "\")
Dim f As New MyFiles
f.Filename = Split (s, "\") (Split (s, "\").Length - 1)
f.CompletePath = s
f.FileSize = My.Computer.FileSystem.GetFileInfo (s).Length
files.Add (f)
Next
ListFiles.DataSource = files
ListFiles.DataBind()
Catch ex As Exception
'Some code for catching exceptions
End Try
End Sub
Hope I solved your problem. I know that you could tweak the code but this had done the job for me. There is another way you can use by storing the files as binary on you database but I choose to do it this way since it's simpler.
The easiest option would probably be to set up a fileshare on the server and then the client app just saves the text file to that shared directory. Or set up a FTP server on the server and send it over via that (or some other similar technology, DAV etc, might be worth asking around on Superuser.com about this since it wouldn't really be a programming question).
Otherwise you could write some kind of webservice or TCP/IP server to run on the server and accept incoming connections from the client and transfer the file via that. But that sounds a bit overkill.
As ho1 mentions, a good option would be to send the text file over FTP. Here is a useful link for FTP'ing from inside your application.
精彩评论