2011-10-26 43 views
0

我想將文件附件保存到網站上的文件夾中。將文件附件保存到搜索文件夾,然後將其路徑寫入數據庫?

我創建該附件的電子郵件的代碼是這樣的:

' Attachments 
If Not (FileUpload1.FileName = "") Then 
    Dim myattachment1 As New Net.Mail.Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.FileName) 
    message.Attachments.Add(myattachment1) 
End If 

我想保存附件爲「C:\ DotNetNuke5 \門戶\ 6個\ JobApplicationDocuments」在服務器上。然後我想將路徑保存到名爲'attachment'的數據庫列中。

我想要一些幫助,或者我可以用Google搜索的東西,因爲我是新手。由於

編輯:現在我有這樣的代碼:

If Not (FileUpload1.FileName = "") Then 
    Dim myattachment1 As New Net.Mail.Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.FileName) 
    message.Attachments.Add(myattachment1) 

    Dim savePath As String = "C:\DotNetNuke5\Portals\6\JobApplicationDocuments\" 
    If (FileUpload1.HasFile) Then 
     savePath += FileUpload1.FileName 
    End If 

End If 

但我不能讓它在該目錄保存。

+1

請將您的答案作爲答案發布,而不是將其編輯到問題中。謝謝! –

+0

完成後,請投我的答案:) – PD24

回答

1

保存上傳的文件,使用此代碼:

 Dim savePath = "C:\DotNetNuke5\Portals\6\JobApplicationDocuments\" 

     Dim uploadedFileName = System.IO.Path.GetFileName(input.FileName) 

     savePath += uploadedFileName 

     Try 

     FileUpload1.PostedFile.SaveAs(savePath) 

     Catch ex As Exception 

     'error. do something 

     End Try 
+0

我已經這樣做了,但謝謝你的解決方案。如果由於硬盤已滿而上傳失敗,您能否提出我可以使用哪種錯誤處理方式?謝謝 – PD24

+0

我不知道如何檢查硬盤是否已滿。但最簡單的方法是將代碼包裝在Try Catch中。更新我的答案,檢查它。 –

1
' Attachments 
     If Not (FileUpload1.FileName = "") Then 
      Dim myattachment1 As New Net.Mail.Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.FileName) 
      message.Attachments.Add(myattachment1) 

      'file path 
      Dim savePath As String = "C:\DotNetNuke5\Portals\6\JobApplicationDocuments\" 
      'check if control has file 
      If (FileUpload1.HasFile) Then 

       'append the applicants email address and the fileupload file name to avoid overwritting same file names. 
       savePath += TxtEmail.Text + "-" + FileUpload1.FileName 

       'attempted to do date time append but unsuccessfull 
       'String.Format("{0}-{1: dd-M-yy}", FileUpload1.FileName, DateTime.Now) 

       'save the file to its path 
       FileUpload1.SaveAs(savePath) 

       'open the connection to the database 
       Dim connectionString As String = "Data Source=192.168.1.0\SQLExpress;Initial Catalog=DNN;User ID=DNNAdmin;Password=password;" 
       Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString) 

       'insert the data 
       Dim queryString As String = "INSERT INTO [tblJobApplication] ([message], [email], [attachment]) VALUES (@message, @email, @attachment)" 
       Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection) 

       'set the parameters 
       sqlCommand.Parameters.Add("@message", System.Data.SqlDbType.NVarChar).Value = message.Body 
       sqlCommand.Parameters.Add("@email", System.Data.SqlDbType.NVarChar).Value = TxtEmail.Text 
       sqlCommand.Parameters.Add("@attachment", System.Data.SqlDbType.NVarChar).Value = savePath 

       Dim rowsAffected As Integer = 0 
       sqlConnection.Open() 
       Try 
        rowsAffected = sqlCommand.ExecuteNonQuery 
       Finally 
        sqlConnection.Close() 
       End Try 


       ' Send the message 
       Dim smtp As System.Net.Mail.SmtpClient = New Net.Mail.SmtpClient() 
       smtp.Host = "localhost" 
       smtp.Send(message) 
       Response.Redirect("applicationcompleted2.htm#top") 
      End If 
      Return 


     End If 

    End If 

以上是我的解決方案。謝謝

我將用戶的電子郵件地址從表單附加到文件附件中,以便它停止用戶上傳cv.doc並覆蓋某人elv cv.doc。它還保存到數據庫的路徑。

相關問題