我正在VB.NET 2005中开发一个桌面应用程序。我有一个要求,用户需要从他的机器(客户端)中选择.txt文件,然后在单击保存按钮后,我需要保存此文件将服务器放入特定文件夹。
同样,我也应该能够检索文件。
请帮助解决此问题。
答案 0 :(得分:2)
这就是我这样做的方式。 您需要的是一个名为“上传”的文件夹。 其余的都是自动完成的。
首先,当用户打开在Page_Load上完成上传的页面时,我会执行以下操作以确保已创建经过身份验证的用户的文件夹。
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
然后在页面上我已经添加了一个ASP:FileUpload控件。 在此控件的Click eveent上,我添加了此代码
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
SaveFile函数具有以下代码
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
并且ListMyFiles函数具有以下代码。
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
希望我解决了你的问题。 我知道你可以调整代码,但这对我来说已经完成了。 您可以通过将文件作为二进制文件存储在数据库中来使用另一种方法,但我选择这样做,因为它更简单。
答案 1 :(得分:0)
最简单的选择可能是在服务器上设置文件共享,然后客户端应用程序只将文本文件保存到该共享目录。或者在服务器上设置一个FTP服务器并通过它(或其他类似的技术,DAV等)发送它可能值得在Superuser.com上询问这个问题,因为它不会是一个编程问题。
否则,你可以编写某种web服务或TCP / IP服务器在服务器上运行,并接受来自客户端的传入连接并通过它传输文件。但这听起来有点矫枉过正。
答案 2 :(得分:0)
正如ho1提到的,一个好的选择是通过FTP发送文本文件。 Here is a useful link用于从应用程序内部进行FTP。