本文介绍了在asp.net C#中从服务器下载文件到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string path = (@"D:\Doc\Offer letter.pdf"); //get physical file path from server
string name = Path.GetFileName(path); //get file name
string ext = Path.GetExtension(path); //get file extension
string type = "";
// set known types based on file extension
if (ext != null)
{
switch (ext.ToLower())
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".txt":
type = "text/plain";
break;
case ".GIF":
type = "image/GIF";
break;
case ".pdf":
type = "Application/pdf";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
}
}
Response.AppendHeader("content-disposition", "attachment; filename=" + name);
if (type != "")
Response.ContentType = type;
Response.WriteFile(path);
Response.End(); //give POP to user for file downlaod
i有这个代码用于从服务器下载文件到客户端。但是这个代码还没有显示任何一个doanload和save的dailogue box.Please任何人帮助我。
i have this code for downloading file from server to client. But this code not yet all showing any dailogue box for doanload and save.Please any one help me.
推荐答案
protected void lnkfilepath_Click(object sender, EventArgs e) // ur link button
{
string filename = lnkfilepath.Text;
string Filpath = Server.MapPath("~/Attachments/" + filename);
DownLoad(Filpath); }
public void DownLoad(string FName){
string path = FName;
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists) {
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream"; // download […]
}
这篇关于在asp.net C#中从服务器下载文件到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!