本文介绍了文件下载代码中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个"shared_files"表,其中有一列"File_path",还有另一列"Download"(下载),我有一个按钮可以下载该特定行的文件.
如何从数据库中检索文件的URL.
我有这段代码.

I had table of "shared_files" and had a column in it "File_path", and had another column Download where i had button to download file of that specific row.
How can i retrieve URL of files from database.
I had this code.

if (e.CommandName == "Download")
        {
            string URL = "";
            FileInfo fileInfo = new FileInfo(URL);

            if (fileInfo.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Flush();
                Response.WriteFile(fileInfo.FullName);
            }
        }



现在,当我点击下载列的下载按钮时,该文件应该存在于同一行中,以进行下载.



Now what should i that when i slick on download button of download column, the file exist in the same row get download.
help me plz.

推荐答案

<asp:linkbutton id="LinkButton1" runat="server" commandargument="<%#Eval("filepath") %>" commandname="Download" xmlns:asp="#unknown">Download</asp:linkbutton>



和后面的代码只使用您写的内容



and code behind just use what u wrote

if (e.CommandName == "Download")
        {
            string URL = e.CommandArgument.Tostring();
            FileInfo fileInfo = new FileInfo(URL);
 
            if (fileInfo.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Flush();
//check your file path is correct or not
                Response.WriteFile(fileInfo.FullName);
//or   Response.WriteFile(Server.MapPath(URL));
 //if url is like ~/Downloadfiles/filename1.pdf
            }
        }



最好的



All the Best


<asp:linkbutton id="Open" runat="server" commandargument="<%# Eval("FilePath") %>" oncommand="Download_Command" text="<%# Bind("FilePath") %>" xmlns:asp="#unknown"></asp:linkbutton>




在代码中




In Code

protected void Download_Command(object sender, CommandEventArgs e)
  {
      try
      {
          string filename = (string)e.CommandArgument;

          string path = Server.MapPath(filename);
          if (File.Exists(path))
          {
              FileInfo toDownload = new FileInfo(path);
              const long ChunkSize = 10000;
              byte[] buffer = new byte[ChunkSize];

              Response.Clear();
              FileStream iStream = File.OpenRead(path);
              long dataLengthToRead = iStream.Length;
              Response.ContentType = ReturnExtension(toDownload.Extension.ToLower());
              Response.AddHeader("content-disposition", "attachment;  filename=" + filename);
              while (dataLengthToRead > 0 & Response.IsClientConnected)
              {
                  int lengthRead = iStream.Read(buffer, 0, 10000);
                  Response.OutputStream.Write(buffer, 0, lengthRead);
                  Response.Flush();
                  dataLengthToRead = dataLengthToRead - lengthRead;
              }
              Response.Close();
          }
      }
      catch (Exception ex)
      {
      }
  }



希望对您有所帮助,
Theingi Win.



Hope be helpful,
Theingi Win.


这篇关于文件下载代码中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 01:03