从gridview下载文件?
问题描述:
我已在数据库中上传了文件路径。我想检索它并保存到磁盘C:
文件路径| datid | filename | timeuploaded |备注| pic_id
DatFiles / 108AB_1D.DAT | 2 | 108AB_1D.DAT |星期五,2014年10月3日上午10:24:31 |样本文件备注| 14529239
我尝试使用此功能但现在确实可以使用..
I have uploaded the filepath on the database. And I wanted to retrieve it and save to Disk C:
File path|datid |filename | timeuploaded| remarks |pic_id
DatFiles/108AB_1D.DAT | 2| 108AB_1D.DAT | Friday, October 03, 2014 10:24:31 AM |Sample File Remarks |14529239
I tried using this but this did now work ..
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
Dim filePath As String = Server.MapPath("C:\")
Dim _DownloadableProductFileName As String = GridView1.Rows(GridView1.SelectedIndex).Cells(2).Text
Dim FileName As New System.IO.FileInfo(filePath & "\" & _DownloadableProductFileName)
Dim myFile As New FileStream(filePath & "\" & _DownloadableProductFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
'Reads file as binary values
Dim _BinaryReader As New BinaryReader(myFile)
Dim startBytes As Long = 0
Dim lastUpdateTiemStamp As String = File.GetLastWriteTimeUtc(filePath).ToString("r")
Dim _EncodedData As String = HttpUtility.UrlEncode(_DownloadableProductFileName, Encoding.UTF8) & lastUpdateTiemStamp
'Clear the content of the response
Response.Clear()
Response.Buffer = False
Response.AddHeader("Accept-Ranges", "bytes")
Response.AppendHeader("ETag", """" & _EncodedData & """")
Response.AppendHeader("Last-Modified", lastUpdateTiemStamp)
'Set the ContentType
Response.ContentType = "application/octet-stream"
'Add the file name and attachment,
'which will force the open/cancel/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment;filename=" & FileName.Name)
'Add the file size into the response header
Response.AddHeader("Content-Length", (FileName.Length - startBytes).ToString())
Response.AddHeader("Connection", "Keep-Alive")
'Set the Content Encoding type
Response.ContentEncoding = Encoding.UTF8
'Send data
_BinaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin)
End Sub
答
你还没有把二进制数据写入Response。
请参考我的提示 - 从ASP.NET 4.0中的GridView行下载文件 [ ^ ]获取完整代码。
You have not written the binary data to Response.
Refer my tip - File download from GridView rows in ASP.NET 4.0[^] for the full code.