2010-09-21 84 views
1

我正在開發VB.NET 2005中的桌面應用程序。我有一個要求,用戶需要從他的機器(客戶端)中選擇一個.txt文件,點擊保存按鈕後,我需要將該文件保存在服務器中的特定文件夾中。在.NET中附加.txt文件

同樣,我應該能夠檢索文件。

請幫忙解決這個問題。

+1

什麼是(在服務器上運行)? – 2010-09-21 09:58:57

回答

0

最簡單的選擇可能是在服務器上設置文件共享,然後客戶端應用程序將文本文件保存到該共享目錄。或者在服務器上建立一個FTP服務器並通過它(或者其他類似的技術,DAV等等,可能值得在Superuser.com上詢問這個問題,因爲它不會是一個編程問題)。

否則,您可以編寫某種web服務或TCP/IP服務器在服務器上運行,並接受來自客戶端的傳入連接並通過該連接傳輸文件。但這聽起來有點矯枉過正。

0

正如ho1提到的,一個好的選擇是通過FTP發送文本文件。 Here is a useful link從您的應用程序中進行FTP'ing。

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 

希望我解決了您的問題。 我知道你可以調整代碼,但這已經爲我完成了這項工作。 還有另外一種方法可以將文件作爲二進制文件存儲在數據庫中,但我選擇這樣做,因爲它更簡單。

+0

我知道'My''命名空間有它的美,但我更喜歡直接導入System.IO'-命名空間並使用'File','Directory'和'Path',節省了大量的輸入。 – Bobby 2010-09-30 08:43:32

+0

另外,您可能想避免使用'Microsoft.VisualBasic'命名空間,它出於兼容性的原因,但是imho無視VB.NET作爲OO語言的全部含義。例如,使用'「MyString」.Split(「。」c)'代替。你也想使用'[System.IO。] Path.Combine(String,String)'來連接路徑。 – Bobby 2010-09-30 08:47:24

+0

感謝您的意見,我會將其考慮在內。 – Besnik 2010-09-30 08:49:50