我该如何下载所有文件? [英] How can i download all file?

查看:103
本文介绍了我该如何下载所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经将excel,word,pdf,notepad,image文件上传到服务器。现在我想下载该文件。

但是当我下载时,某些文件类型(excel,word,notepad)不会显示它们的图标。它只显示硬盘。

这里是我的源代码下载:



Hi all experts,
I have uploaded excel,word,pdf,notepad,image file to server. Now i want to download that file.
but when i download, some file type (excel,word,notepad) don''t display their icon. it just display the harddrive.
here are my source code to download:

String fileName = "English Course.xls";
Response.ContentType="application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.TransmitFile(Server.MapPath("~/SAP/" + fileName));
Response.End();





有谁知道如何下载所有类型的文件?



谢谢

TONY



Does anybody know how to download all type of file?

Thanks
TONY

推荐答案

private void DownloadFile(string filename)
   {
       try
       {
           //Fileinfo class get all Details about user select file.
           FileInfo file = new FileInfo(filename);
           //Checking File is Exists are not using FileInfo object.Exists properties.
           if (file.Exists)
           {
               //Clears all content output from buffer stream.
               Response.ClearContent();
               //ContentType is used to get & set HTTP MIME type of the output stream
               Response.ContentType = "application/octet-stream";
               //AddHeader method is used addHTTP header to the output stream.
               Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
               Response.AddHeader("Content-Length", file.Length.ToString());
               Response.WriteFile(file.FullName);
               //Send all Current buffered output to the client.
               Response.Flush();
               //Clears all Content output from the buffer stream.
               Response.Clear();
           }

       }
       catch (Exception en)
       {
           Response.Write(en.Message.ToString());

       }
   }


亲爱的朋友,



分析代码。



private void loaddetails()

{

试试

{

//objinfo.IntInformationID = Convert.ToInt32(Request.QueryString [id]);

objinfo.IntInformationID = Convert.ToInt32(Session [ id]。ToString());

DataSet ds = objinfo.GetInformationPdf();

if(ds.Tables [0] .Rows.Count> 0)

{

尝试

{

ViewState [filename] = ds.Tables [0] .Rows [0] [strDocument]。ToString();

string strPath = string.Empty;

strPath = Server.MapPath(Documents)+\ \+ ViewState [filename]。ToString();

FileInfo file = new FileInfo(strPath);
if(file.Exists)

{



Response.ClearContent();

Response.AddHeader(Content-Disposition,filename =+ file.Name);

Response.AddHeader(Content-Length,file.Length.ToString()); < br $>


string [] type = ViewState [filename]。ToString()。Split(''。'');

if( type [1] ==doc)

{

Response.ContentType =application / msword;

}

else if(type [1] ==docx)

{

Response.ContentType =application / vnd.msword.document.12;

}

else if(type [1] == pdf)

{

Response.ContentType =application / pdf;

}

文件。 IsReadOnly = true;

Response.TransmitFile(file.FullName);



Response.End();









}

}

catch(Exception ex)

{

Page.RegisterClientScriptBlock(pageClose,< script> alert(''Document Not Available'');< /脚本>);

}

}



}

catch(例外情况ex)

{

}

}
Dear Friend,

Analyze the code.

private void loaddetails()
{
try
{
//objinfo.IntInformationID = Convert.ToInt32(Request.QueryString["id"]);
objinfo.IntInformationID = Convert.ToInt32(Session["id"].ToString());
DataSet ds = objinfo.GetInformationPdf();
if (ds.Tables[0].Rows.Count > 0)
{
try
{
ViewState["filename"] = ds.Tables[0].Rows[0]["strDocument"].ToString();
string strPath = string.Empty;
strPath = Server.MapPath("Documents") + "\\" + ViewState["filename"].ToString();
FileInfo file = new FileInfo(strPath);
if (file.Exists)
{

Response.ClearContent();
Response.AddHeader("Content-Disposition", "filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());

string[] type = ViewState["filename"].ToString().Split(''.'');
if (type[1] == "doc")
{
Response.ContentType = "application/msword";
}
else if (type[1] == "docx")
{
Response.ContentType = "application/vnd.msword.document.12";
}
else if (type[1] == "pdf")
{
Response.ContentType = "application/pdf";
}
file.IsReadOnly = true;
Response.TransmitFile(file.FullName);

Response.End();




}
}
catch (Exception ex)
{
Page.RegisterClientScriptBlock("pageClose", "<script>alert(''Document Not Available'');</script>");
}
}

}
catch (Exception ex)
{
}
}


在上述解决方案中如果Response.Wri teFile不起作用;然后尝试使用相同参数的Response.Redirect。
In the Above Solutions if "Response.WriteFile" doesnot works; then try "Response.Redirect" with same parameters.


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

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