从gridview下载文件? [英] Download a file from gridview?

查看:50
本文介绍了从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.


这篇关于从gridview下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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