将上传的文件保存到数据库vb.net [英] save uploaded files to database vb.net

查看:87
本文介绍了将上传的文件保存到数据库vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

受保护的子Page_Load(ByVal sender As Object,ByVal e As System.EventArgs)处理Me.Load 
Email.Attributes(type)=email
结束子

受保护子联系人Usclick()
尝试
如果ctrlGoogleReCaptcha.Validate则
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。值
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 strFi leName 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,服务请求已收到,正文,附件列表,没什么)

'BusinessLogic.Utilities.MailingFunctions.SendSMTPContactRequest(Phiston Feedback Mailer&< & ConfigurationManager.AppSettings(ContactUSFromEmailAddress) & > ,_
'ConfigurationManager.AppSettings(ServiceContactToEmailAddress),服务请求已收到,正文,附件列表,没有)

Response.Redirect(contactthanks.aspx)
否则
ErrorLbl.Visible = True
结束如果
Catch ex As Exception
Throw
结束尝试
结束子


公共共享函数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(< bgcolor =' #33ffff' width < span class =code-keyword> =' 100%' border =' 1' cellpadding =' 0' cellspacing =' < span class =code-keyword> 0' >
builder.Append(< tr >
builder.Append( < td width =' 100%' align =' center' colspan =' 2' bgcolor =' #00bbff' >
builder.Append (< B > Phiston联系我们< / B >
builder.Append(< / td >
builder.Append(< / tr >


builder.Append(< tr >
builder.Append(< span class =code-keyword>< td style =' width:20%' > < B > 日期< / B > < / td >
builder .Append(< td < span class =code-attribute> style =' width:80%' < span class =code-keyword>> & nbsp;)
builder.Append(Now()。ToLongDateString& & Now()。ToLongTimeString)
builder.Append(< / td >
builder.Append(< / tr >

builder.Append(< tr >
builder.Append(< td 样式 =' width:20%' > < B > 数据库名称< / B > < /跨度> < / td >
builder.Append(< td 样式 =' 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 > 信息< / B > < / td >
builder.Append(< / tr >

builder.Append( < tr >
builder.Append(< td 样式 =' width:20%' align =' < span class =code-keyword> left' > < B > 全名< / B > < / td >
builder.Append(< td style =' width:80%' align =' left' > & FullName& < / td >
builder.Append(< / tr >

builder.Append(< tr >
builder.Append(< td style =' width:20%' 对齐 =' left' > < B > 公司名称< / B > < / td >
builder.Append(< td 样式 =' width:80%' align =' left' > &公司名称& < / td >
builder.Append(< / tr >

builder.Append(< tr >
builder.Append(< td style =' width:20%' 对齐 =' left' > < B > 电子邮件< / B > < / td >
builder.Append (< td 样式 =' width:80%' align =' left' > &电子邮件& < / td >
builder.Append(< / tr >

builder.Append(< tr >
builder.Append(< td style =' width:20%' 对齐 =' left' > < B > 电话< / B > < / td >
builder.Append (< td 样式 =' width:80%' align =' left' > &电话& < / td >
builder.Append(< / tr >

builder.Append(< tr >
builder.Append(< td style =' width:20%' 对齐 =' left' > < B > Serial#< / B > < / td >
builder.Append(< td 样式 =' width:80%' align =' left' > &串行和< / td >
builder.Append(< / tr >

builder.Append(< tr >
builder.Append(< td style =' width:20%' 对齐 =' left' > < B > 产品地址< / B > < / td >
builder.Append(< td 样式 =' width:80%' align =' left' > &产品地址& < / td >
builder.Append(< / tr >

builder.Append(< tr >
builder.Append(< td style =' width:20%' 对齐 =' left' > < B > 评论< / B > < / td >
builder .Append(< td < span class =code-attribute> style =' width:80%' < span class =code-attribute> align =' left' > &评论& < / td >
builder.Append(< / tr >


builder.Append(< tr >
builder.Append(< td style =' width:20%' align =' left' > < B > 文件附加ed < / B > < span class =code-keyword>< / td >
builder.Append(< td 样式 =' width:80%' align =' left' > & AttachmentCount& < / td >
builder.Append(< / tr >

返回builder.ToString

Catch e As Exception
抛出
结束尝试

结束函数

公共共享子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)

尝试
Dim insMail As New MailMessage(新邮件地址(strFrom),新邮件地址(strTo))
insMail.Priority = MailPriority.High
insMail.IsBodyHtml = True
使用insMail
.Subject = strSubject
.Body = strBody
如果不是(IsNothing(CC))则
.CC。添加(新邮件地址(CC))
结束如果

如果不是IsNothing(附件)AndAlso Attachments.Count < > 0然后
For i As Integer = 0 To Attachments.Count - 1
.Attachments.Add(附件(i))
下一个
结束如果



结束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例外
抛出
结束尝试
结束子

解决方案

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



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



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

  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


这篇关于将上传的文件保存到数据库vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆