部门的一个项目,我的任务是负责为移动终端提供数据。
数据本来有PDF,TXT.DOC格式的文档,然后现在TXT的没问题,PDF的下载就出现异常。代码如下: WebClient wb = new WebClient( ); byte[] bytes = wb.DownloadData(attachFullName); this._context.Response.Clear();
this._context.Response.BufferOutput = true;
this._context.Response.Buffer = false;
this._context.Response.Charset = "GB2312";
this._context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//通知浏览器下载文件而不是打开
//this._context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(attCName, System.Text.Encoding.UTF8));
this._context.Response.AddHeader("Content-Disposition", "inline;filename=" + HttpUtility.UrlEncode(attCName, System.Text.Encoding.UTF8));
string fileType = attCName.EndsWith("txt") == true ? "txt" : attCName.EndsWith("pdf") == true ? "pdf" : attCName.EndsWith("doc") == true ? "word" : "pdf";
switch (fileType)
{
case "pdf":
{
this._context.Response.ContentType = "application/pdf";
break;
}
case "word":
{
this._context.Response.ContentType = "application/msword";
break;
}
case "txt":
default:
{
this._context.Response.ContentType = "text/plain";
break;
}
}
//this._context.Response.ContentType = "application/octet-stream";
this._context.Response.BinaryWrite(bytes);
this._context.Response.Flush( );
this._context.Response.Close( );异常的详细信息是:System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.AbortInternal()
at System.Threading.Thread.Abort(Object stateInfo)
at System.Web.HttpResponse.End()
at WBS.KingtonNewsMobileWeb.NewsDetailsDownloadHandler.DownLoadFile()
at WBS.KingtonNewsMobileWeb.NewsDetailsDownloadHandler.ProcessRequest(HttpContext context)
在线等急救啊。
at System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[] status, Byte[] header, Int32 keepConnected, Int32 totalBodySize, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32 doneWithSession, Int32 finalStatus, Boolean& async)
at System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean isFinal)
at System.Web.HttpResponse.Flush(Boolean finalFlush)
at System.Web.HttpResponse.BinaryWrite(Byte[] buffer)
at WBS.KingtonNewsMobileWeb.NewsDetailsDownloadHandler.DownLoadFile()
at WBS.KingtonNewsMobileWeb.NewsDetailsDownloadHandler.ProcessRequest(HttpContext context)
有2个注意点:
1.iPhone的客户端和Android的客户端的处理方式有所不同。PDF的文件,iPhone可以直接打开,android就只能先下载。
2.WebClient下载文件时,设置格式为 GB2312