本文介绍了将上传的文件保存到数据库vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 受保护的子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 =' 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联系我们< / B > ) builder.Append(< / td > ) builder.Append(< / tr > ) builder.Append(< tr > ) builder.Append(< td style =' width:20%' > < B > 日期< / 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 样式 =' 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 =' 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 style =' width:80%' align =' left' > &评论& < / td > ) builder.Append(< / tr > ) builder.Append(< tr > ) builder.Append(< td style =' width:20%' align =' left' > < B > 文件附加ed < / B > < / 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.LoadEmail.Attributes("type") = "email"End SubProtected Sub ContactUsclick()TryIf ctrlGoogleReCaptcha.Validate ThenDim ID = System.Guid.NewGuidDim FullNameText = Trim(FullName.Value)Dim CompanyNameText = Trim(CompanyName.Value)Dim EmailText = Trim(Email.Value)Dim PhoneText = Phone.ValueDim SerialNumberText = SerialNumber.ValueDim ProductLocationAddress = ProductLocation.ValueDim 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))NextDim 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")ElseErrorLbl.Visible = TrueEnd IfCatch ex As ExceptionThrowEnd TryEnd SubPublic 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)TryDim builder As StringBuilder = New StringBuilderDim 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%'> ")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%'> ")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.ToStringCatch e As ExceptionThrowEnd TryEnd FunctionPublic 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)TryDim insMail As New MailMessage(New MailAddress(strFrom), New MailAddress(strTo))insMail.Priority = MailPriority.HighinsMail.IsBodyHtml = TrueWith insMail.Subject = strSubject.Body = strBodyIf Not (IsNothing(CC)) Then.CC.Add(New MailAddress(CC))End IfIf Not IsNothing(Attachments) AndAlso Attachments.Count <> 0 ThenFor i As Integer = 0 To Attachments.Count - 1.Attachments.Add(Attachments(i))NextEnd IfEnd WithDim smtp As New System.Net.Mail.SmtpClientsmtp.EnableSsl = Falsesmtp.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 ExceptionThrowEnd TryEnd 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.InputStreamDim 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!