我想将文件附件保存到网站上的文件夹中。
我创建电子邮件附件的代码如下:
' 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 \ Portals \ 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
但我无法将其保存在目录中。
答案 0 :(得分: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
答案 1 :(得分: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并覆盖别人cv.doc。它还保存了数据库的路径。