且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将上传的文件保存到数据库vb.net

更新时间:2022-10-25 08:05:45

您从流中读取字节数保存在数据库中。这将流的当前位置设置为结束。



当附件尝试从流中读取内容时,它会读取零字节,因为没有更多数据可供读取。结果,你得到空的附件。



你需要在阅读之后重置流的位置:

  Dim  fs  As  Stream = UploadedFile.InputStream 
Dim br As BinaryReader(fs)
DataAccessLayer.DataAccess.InsertServiceRequestAttachments(ServiceQueueUniqueIdentifier,ID,UploadedFile.FileName,UploadedFile.ContentType,br.ReadBytes(fs.Length))

' 添加以下行:
fs.Position = 0


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Email.Attributes("type") = "email"
End Sub

Protected Sub ContactUsclick()
Try
If ctrlGoogleReCaptcha.Validate Then
Dim ID = System.Guid.NewGuid
Dim FullNameText = Trim(FullName.Value)
Dim CompanyNameText = Trim(CompanyName.Value)
Dim EmailText = Trim(Email.Value)
Dim PhoneText = Phone.Value
Dim SerialNumberText = SerialNumber.Value
Dim ProductLocationAddress = ProductLocation.Value
Dim CommentText = Trim(Comment.Value)

DataAccessLayer.DataAccess.InsertServiceRequest(ID, FullNameText, CompanyNameText, EmailText, _
PhoneText, SerialNumberText, ProductLocationAddress, CommentText)

Dim AttachmentList As New List(Of Attachment)

For Each UploadedFile As HttpPostedFile In FileUpload.PostedFiles
    Dim ServiceQueueUniqueIdentifier = System.Guid.NewGuid
    Dim strFileName As String = System.IO.Path.GetFileName(UploadedFile.FileName)
    Dim attachment As New Attachment(UploadedFile.InputStream, strFileName)
    AttachmentList.Add(attachment)

    Dim fs As Stream = UploadedFile.InputStream
    Dim br As New BinaryReader(fs)
'Dim bytes As Byte() = br.ReadBytes(fs.Length)
    DataAccessLayer.DataAccess.InsertServiceRequestAttachments(ServiceQueueUniqueIdentifier, ID, UploadedFile.FileName, UploadedFile.ContentType, br.ReadBytes(fs.Length))

Next


Dim body As String = BusinessLogic.Utilities.MailingFunctions.FormatServiceRequestToXML(FullNameText, CompanyNameText, EmailText, PhoneText, _
SerialNumberText, ProductLocationAddress, CommentText, AttachmentList.Count)

BusinessLogic.Utilities.MailingFunctions.SendSMTPContactRequest("""Phiston Feedback Mailer""" & "<" & ConfigurationManager.AppSettings("ContactUSFromEmailAddress") & ">", _
"Shahrouz.ebadian@gmail.com", "Service Request Received", body, AttachmentList, Nothing)

'BusinessLogic.Utilities.MailingFunctions.SendSMTPContactRequest("""Phiston Feedback Mailer""" & "<" & ConfigurationManager.AppSettings("ContactUSFromEmailAddress") & ">", _
'                                          ConfigurationManager.AppSettings("ServiceContactToEmailAddress"), "Service Request Received", body, AttachmentList, Nothing)

Response.Redirect("contactthanks.aspx")
Else
ErrorLbl.Visible = True
End If
Catch ex As Exception
Throw
End Try
End Sub


Public Shared Function FormatServiceRequestToXML(ByVal FullName As String, ByVal CompanyName As String, ByVal Email As String, ByVal Phone As String, _
ByVal Serial As String, ByVal ProductAddress As String, ByVal comment As String, ByVal AttachmentCount As Integer)
Try

Dim builder As StringBuilder = New StringBuilder
Dim body As String = ""

builder.Append("<table bgcolor='#33ffff' width='100%' border='1' cellpadding='0' cellspacing='0'>")
builder.Append("<tr>")
builder.Append("<td width='100%' align='center' colspan='2' bgcolor='#00bbff'>")
builder.Append("<B>Phiston Contact Us</B>")
builder.Append("</td>")
builder.Append("</tr>")


builder.Append("<tr>")
builder.Append("<td style='width: 20%'><B>Date</B></td>")
builder.Append("<td style='width: 80%'>&nbsp;")
builder.Append(Now().ToLongDateString & " " & Now().ToLongTimeString)
builder.Append("</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%'><B>DB Name</B></td>")
builder.Append("<td style='width: 80%'>&nbsp;")
builder.Append(BusinessLogic.SystemInfo.SystemInformation.GetDBName)
builder.Append("</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 100%' align='center' colspan='2' bgcolor='#00bbff'><B>Information</B></td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Full Name</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & FullName & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Company Name</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & CompanyName & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Email</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & Email & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Phone</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & Phone & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Serial #</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & Serial & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Product Address</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & ProductAddress & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Comment</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & comment & "</td>")
builder.Append("</tr>")


builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Files Attached</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & AttachmentCount & "</td>")
builder.Append("</tr>")

Return builder.ToString

Catch e As Exception
Throw
End Try

End Function

Public Shared Sub SendSMTPContactRequest(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String, _
ByVal Attachments As List(Of Attachment), ByVal CC As String)

Try
Dim insMail As New MailMessage(New MailAddress(strFrom), New MailAddress(strTo))
insMail.Priority = MailPriority.High
insMail.IsBodyHtml = True
With insMail
.Subject = strSubject
.Body = strBody
If Not (IsNothing(CC)) Then
.CC.Add(New MailAddress(CC))
End If

If Not IsNothing(Attachments) AndAlso Attachments.Count <> 0 Then
For i As Integer = 0 To Attachments.Count - 1
.Attachments.Add(Attachments(i))
Next
End If


End With
Dim smtp As New System.Net.Mail.SmtpClient

smtp.EnableSsl = False
smtp.Port = ConfigurationManager.AppSettings("SmtpPort")
smtp.Host = ConfigurationManager.AppSettings("SmtpHost")
smtp.Credentials = New System.Net.NetworkCredential(ConfigurationManager.AppSettings("ContactUsFromEmailAddress"), ConfigurationManager.AppSettings("ContactUSFromEmailPassword"))
smtp.Send(insMail)

Catch e As Exception
Throw
End Try
End Sub

You read the bytes from the stream to save in the database. That sets the current position of the stream to the end.

When the attachment tries to read the content from the stream, it reads zero bytes, because there is no more data to read. As a result, you get empty attachments.

You need to reset the stream's position after you've read it:
Dim fs As Stream = UploadedFile.InputStream
Dim br As New BinaryReader(fs)
DataAccessLayer.DataAccess.InsertServiceRequestAttachments(ServiceQueueUniqueIdentifier, ID, UploadedFile.FileName, UploadedFile.ContentType, br.ReadBytes(fs.Length))

' Add this line:
fs.Position = 0