public void DownloadFile(string fileId)
{
//Stream fileStream = null;
try
{
int fileID = Convert.ToInt32(fileId);
string RelatePath = fileInfoBLL.GetRelatePath(fileID);
string fileFullPath = filePath + "\\" + RelatePath;
string fileName = fileInfoBLL.GetFileName(fileID);
var response = HttpContext.Current.Response;
response.Clear();
response.Buffer = true;
//fileName = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("UTF-8"));
response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
response.ContentEncoding = System.Text.Encoding.UTF8;
response.Charset = "UTF-8";
response.WriteFile(fileFullPath);
}
catch (Exception ex)
{
ExceptionHandler.ExceptionHelper.Instance.HandleException(ex);
}
}

将字符串转换成指定编码格式:

   string fileName = System.Web.HttpUtility.UrlEncode("要转换的字符串", System.Text.Encoding.GetEncoding("UTF-8"));

2    string gbStr =  System.Text.Encoding.GetEncoding("gb2312").GetString(System.Text.Encoding.Default.GetBytes('xxx'));

另一种:

  private void DownloadFile(string fileName, string filePath)
{
try
{
if (!string.IsNullOrEmpty(filePath))
{
if (string.IsNullOrEmpty(fileName))
{
fileName = filePath.Substring(filePath.LastIndexOf("\\") + );
} context.Response.Clear();
context.Response.Buffer = true;
context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName));
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Charset = "UTF-8";
//context.Response.ContentType = "application/vnd.ms-excel";
context.Response.WriteFile(UploadFileSavePath + "\\" + filePath);
}
}
catch (Exception ex)
{
ExceptionHelper.Instance.HandleException(ex);
context.Response.Write("{\"bizSuccess\":false,\"msg\":\"下载文件时发生错误!\"}");
} context.Response.Flush();
context.Response.End();
}

利用WebOperationContext.Current.OutgoingResponse下载

public Stream DownloadFile(string token)
{
Stream fileStream = null;
try
{
//FilesInfo filesInfo = m_FileDAL.Find<FilesInfo>(fileId);
//string fileFullPath = filesInfo.FileUrl + filesInfo.FileName;
//string fileFullPath = m_FileDAL.Find<FilesInfo>(fileId).FileUrl;
//string fileFullPath = @"C:\Users\chen\Desktop\reader.txt";
string fileFullPath = @"C:\Users\chen\Desktop\HSF第四次读后感.docx";
//string fileName = "reader.txt";
string fileName = "HSF第四次读后感.docx";
fileStream = File.OpenRead(fileFullPath);
var response = WebOperationContext.Current.OutgoingResponse;
response.Headers.Add("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName));
response.ContentType = "application/octet-stream";
//var message = WebOperationContext.Current.CreateStreamResponse(fileStream, "application/octet-stream");
//message.Headers.Add(System.ServiceModel.Channels.MessageHeader.CreateHeader("Content-Disposition", "", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName)));
//response.Clear();
//response.Buffer = true;
//response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName));
//response.ContentEncoding = System.Text.Encoding.UTF8;
//response.Charset = "UTF-8"; //result.Success = true;
//result.ErrorMessage = string.Format("{0}下载成功!", fileId);
//result.Data = fileStream;
}
catch (Exception ex)
{ //result.Success = false;
//result.ErrorMessage = string.Format("{0}下载失败!", fileId);
//result.Data = null;
} return fileStream;
}

Rest文件下载的更多相关文章

  1. Android 浏览器 —— 使用 WebView 实现文件下载

    对当前的WebView设置下载监听 mCurrentWebView.setDownloadListener(new DownloadListener() { @Override public void ...

  2. C# 文件下载 : WinINet

    在 C# 中,除了 WebClient 我们还可以使用一组 WindowsAPI 来完成下载任务.这就是 Windows Internet,简称 WinINet.本文通过一个 demo 来介绍 Win ...

  3. ASP.net MVC 文件下载的几种方法(欢迎讨论)

    在ASP.net MVC 中有几种下载文件的方法 前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的. 第一种:最简单的超链接方法,&l ...

  4. 让IIS7.0.0.0支持 .iso .7z .torrent .apk等文件下载的设置方法

    IIS默认支持哪些MIME类型呢,我们可以这样查看:打开IIS管理器(计算机--管理--服务和应用程序--Internet信息服务(IIS)管理器:或者Win+R,输入inetmgr,Enter),在 ...

  5. Android中使用AsyncTask实现文件下载以及进度更新提示

    Android提供了一个工具类:AsyncTask,它使创建需要与用户界面交互的长时间运行的任务变得更简单.相对Handler来说AsyncTask更轻量级一些,适用于简单的异步处理,不需要借助线程和 ...

  6. 利用Tomcat内置的servlet实现文件下载功能

    起因 最近博客所在的VPS挂了又要重装系统,又要重装各种软件. 以前我也经常更换VPS,每次更换都是各种坑爹事情..比如要下载java.下载tomcat.下载mysql..........以前每次我都 ...

  7. 多个文件下载打包生成zip格式下载

    这个多个文件下载生成zip格式必须先引用一个ICSharpCode.SharpZipLib.dll. 代码如下  //将多个文件打包成压缩文件zip格式下载         protected voi ...

  8. .net一般处理程序(httphandler)实现文件下载功能

    Handler文件代码如下: public class MDMExporterWeb : IHttpHandler { public void ProcessRequest(HttpContext c ...

  9. asp.net 文件下载(txt,rar,pdf,word,excel,ppt)

    aspx 文件下载说起来一点都不难,但是在做的过程中还是遇到了一些小小的问题,就是因为这些小小的问题,导致解决起来实在是太难了,其中一个就是Response.End();导致下载文件出现线程终止的情况 ...

  10. JavaScript多文件下载

    对于文件的下载,可以说是一个十分常见的话题,前端的很多项目中都会有这样的需求,比如 highChart 统计图的导出,在线图片编辑中的图片保存,在线代码编辑的代码导出等等.而很多时候,我们只给了一个链 ...

随机推荐

  1. 51nod 1613翻硬币

    题目链接:51nod 1613 翻硬币 知乎上的理论解法http://www.zhihu.com/question/26570175/answer/33312310 本题精髓在于奇偶性讨论. 若 n ...

  2. 11 自定制shell提示符

    shell提示符  huiubantu@ubuntu:~$ shell提示符保存在PS1变量中 包括用户名,主机名,当前工作目录 可以通过echo命令查看PS1的内容 huiubantu@ubuntu ...

  3. eclipse 安装git的插件和上传项目

    这里有个链接,已经很详细的写了过程  博客1以及博客2.其实遇到安装的问题,就是因为我用的eclipse版本比较老,但是eclipse里面又装了好多插件,不想在重新安装eclipse.还有一个很好的博 ...

  4. PHP基本知识收集

    1.符号“@”的作用 @是可以屏蔽函数执行过程中遇到问题而产生的一些错误.警告信息,这样用户就看不到程序的出错信息.这样除了用户界面会友好一些外,更重要的是安全性,因为屏蔽了出错文件的路径等信息. 2 ...

  5. eclipse-mysql-tomcat bug之旅

    赶紧默念三遍google大法好... [连接数据库 servlet调用提示找不到可加载的driver,普通的.java文件没问题] 表示不服啊...明明可以连上啊...为什么多了几个中间界面就不好使了 ...

  6. 让你的WPF程序使用多线程——BackgroundWorker

    在wpf中可以使用许多方法执行异步操作.利用.NET的芳芳就是手动创建一个新的System.Threading.Thread对象,提供一步代码,并使用THread.Start()方法加载代码.这种方法 ...

  7. git常用语法

    git笔记  1.     获取项目(克隆):     Git repository:                       git clone -b develop ssh://git@68. ...

  8. 53个要点提高PHP编程效率

    1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row[’id’] 的速度是$row[id]的7倍.3.echo 比 print 快,并且使用echo ...

  9. Understanding Weak References

    Understanding Weak References Posted by enicholas on May 4, 2006 at 5:06 PM PDT Some time ago I was ...

  10. submit回车提交影响

    $(".bInput").bind('keydown',function(event){//回车提交手动标签 if(event.keyCode==13){              ...