关于网友提出的“急急急急 关于下载文件的问题!!急!!求助高手呀!!!”问题疑问,本网通过在网上对“急急急急 关于下载文件的问题!!急!!求助高手呀!!!”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:急急急急 关于下载文件的问题!!急!!求助高手呀!!!
描述: string url = Execute("select url from Table where id=" + Request["id"] + "").ToString();
System.IO.FileInfo file = new System.IO.FileInfo(url);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(MapPath(url)));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush();
Response.End();
文件上传上来是没有问题的
但是通过这种方式下载
下下来的 除了txt可以打开
其他的全是坏文件
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
这两行我也改了
不好使啊?
解决方案1: FileStream fileStream=new FileStream(filepath, FileMode.Open);
long fileSize = fileStream.Length;
Context.Response.C/octet-stream";
Context.Response.AddHeader("Content-Disposition","attachment; filename=\"" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + "\"");
Context.Response.AddHeader("Content-Length",fileSize.ToString());
byte[] fileBuffer=new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
fileStream.Close();
Context.Response.BinaryWrite(fileBuffer);
Context.Response.End();
解决方案2: /**////
/// To download a file using Response.WriteFile(string filename)
///
///
public static void Download(string path)
{
try
{
if(path!=null)
{
FileInfo fi = new FileInfo(path);
HttpContext.Current.Response.Clear();
// //当要下载的文件名是中文时,需加上HttpUtility.UrlEncode
HttpContext.Current.Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(fi.Name));
HttpContext.Current.Response.AddHeader("Content-Length",fi.Length.ToString());
HttpContext.Current.Response.C/octet-stream";
HttpContext.Current.Response.WriteFile(fi.FullName);
HttpContext.Current.Response.End();
}
else
{
CommonFunction.MessageBox("File Does not exist!");
}
}
catch(Exception _ex)
{
throw _ex;
}
}
以上介绍了“急急急急 关于下载文件的问题!!急!!求助高手呀!!!”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3048608.html