所以,我有一个web应用程序,将一些附件保存到SQL数据库作为VarBinary(max)
,一切工作顺利,直到我意识到,当我从数据库下载附件时,文件与原始大小相比非常大,所以一个400KB的文件,下载的一个将是5MB!我做错了什么?
上传代码为:
Dim img As FileUpload = CType(FileUpload1, FileUpload)
Dim imgByte As Byte() = Nothing
If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
Dim File As HttpPostedFile = FileUpload1.PostedFile
imgByte = New Byte(File.ContentLength - 1) {}
File.InputStream.Read(imgByte, 0, File.ContentLength)
cmd.Parameters.AddWithValue("@filetype", Path.GetExtension(FileUpload1.FileName).ToLower)
cmd.Parameters.AddWithValue("@attachment", imgByte)
End If
下载代码:
Dim imagem As Byte() = CType((dr("attachment")), Byte())
Response.Clear()
Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0")
Response.AddHeader("Pragma", "no-cache")
Response.AddHeader("Content-Description", "File Download")
Response.AddHeader("Content-Type", "application/force-download")
Response.AddHeader("Content-Transfer-Encoding", "binaryn")
Dim fileName As String = "attachment; filename=" & "DOC_REFNO_" & "_" & regref_txt.Text.Replace("/", "_") & dr("filetype")
Response.AddHeader("content-disposition", fileName)
Response.BinaryWrite(imagem)
谢谢大家。
所以我发现下载代码不正确,我把它改成下面的代码,工作得很好。
Dim imagem As Byte() = CType((dr("attachment")), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = ContentType
Dim fileName As String = "attachment; filename=" & "DOC_REFNO_" & "_" & regref_txt.Text.Replace("/", "_") & dr("filetype")
Response.AddHeader("content-disposition", fileName)
Response.BinaryWrite(imagem)
Response.Flush()
Response.End()