/// <summary> /// Builds a response that will return a binary file to the client. /// </summary> /// <param name="response">The <see cref="HttpResponse"/> that acts as the this instance.</param> /// <param name="fileName">The name of the file.</param> /// <param name="fileContents">The file contents.</param> /// <param name="encoding">The character encoding of the file. Defaults to <see cref="UTF8Encoding"/>.</param> /// <param name="contentType">The Internet Media Type. See <see cref="InternetMediaType"/> for a valid list of values.</param> public static void BuildBinaryFileResponse(this HttpResponse response, string fileName, byte[] fileContents, Encoding encoding, string contentType = InternetMediaType.Null) { response.AddHeader(HttpResponseHeader.ContentDispositionKey, string.Format(HttpResponseHeader.ContentDispositionValueFormat, HttpUtility.UrlPathEncode(fileName))); response.AddHeader(HttpResponseHeader.ContentLengthKey, fileContents.Length.ToString()); response.AddHeader(HttpResponseHeader.PragmaKey, HttpResponseHeader.PragmaValuePublic); response.ContentType = contentType; response.Charset = encoding.HeaderName; response.ContentEncoding = encoding; response.BinaryWrite(fileContents); }
public static void DownloadFile(this HttpResponse source, DownloadableFile file) { var contentData = file.ContentData.IsEmpty() ? file.ContentText.GetBytes(source.ContentEncoding) : file.ContentData; source.Clear(); source.Buffer = false; source.AddHeader("Accept-Ranges", "bytes"); source.AddHeader("Content-Disposition", "attachment;filename=\"" + file.FileName + "\""); source.AddHeader("Connection", "Keep-Alive"); source.ContentType = file.ContentType; source.BinaryWrite(contentData); source.End(); }
public static void WriteStreamToResponce(this HttpResponseBase response, Stream stream) { //set unbuffered output response.Buffer = false; response.BufferOutput = false; if (stream.CanSeek) { stream.Seek(0, SeekOrigin.Begin); } var buffer = new byte[BufferReadLength]; int readed; while ((readed = stream.Read(buffer, 0, BufferReadLength)) > 0) { var subbufer = new byte[readed]; Array.Copy(buffer, subbufer, readed); response.BinaryWrite(subbufer); } }
/// <summary> /// 通用下载方法,不限速 /// </summary> /// <param name="response">HttpResponse</param> /// <param name="fullPath">文件全路径</param> /// <param name="fileName">保存的文件名,可空</param> /// <returns></returns> public static bool ResponseFile(this HttpResponse response, string fullPath,string fileName="") { string strFileName = new FileInfo(fullPath).Name; if (!string.IsNullOrEmpty(fileName)) { strFileName = fileName; } try { FileStream myFile = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(myFile); try { response.AddHeader("Accept-Ranges", "bytes"); response.Buffer = false; long fileLength = myFile.Length; long startBytes = 0; response.AddHeader("Content-Length", (fileLength - startBytes).ToString()); if (startBytes != 0) { response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength)); } response.AddHeader("Connection", "Keep-Alive"); response.ContentType = "application/octet-stream"; response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8)); int pack = 10240; //10K bytes 进行拆包,每包大小 byte[] buff = new byte[pack]; var contentLength = br.Read(buff, 0, pack); while (contentLength != 0) { if (response.IsClientConnected) { response.BinaryWrite(buff); response.Flush(); contentLength = br.Read(buff, 0, pack); } else { break; } } } catch { return false; } finally { br.Close(); myFile.Close(); } } catch { return false; } return true; }
/// <summary> /// 输出Json文本到客户端 /// </summary> /// <param name="Response"></param> /// <param name="json"></param> public static void WriteJson(this HttpResponseBase Response, IJson json) { Response.ContentType = "application/json"; Response.ClearContent(); Response.BinaryWrite(Encoding.UTF8.GetBytes(json.ToString())); Response.Flush(); }
/// <summary> /// 输出纯文本到客户端 /// </summary> /// <param name="Response"></param> /// <param name="text"></param> public static void WriteText(this HttpResponseBase Response, string text) { Response.ContentType = "text/plain"; Response.ClearContent(); Response.BinaryWrite(Encoding.UTF8.GetBytes(text)); Response.Flush(); }
/// <summary> /// 输出Jsonp文本到客户端 /// </summary> /// <param name="Response"></param> /// <param name="callBack">客户端的js回调方法</param> /// <param name="json">json参数</param> public static void WriteJsonp(this HttpResponseBase Response, string callBack, IJson json) { Response.ContentType = "application/x-javascript"; Response.ClearContent(); Response.BinaryWrite(Encoding.UTF8.GetBytes(string.Format("{0}({1});", callBack, json.ToString()))); Response.Flush(); }
public static void CreatePDF(this HttpResponse response, byte[] pdf, string filename) { response.ClearContent(); response.ClearHeaders(); response.ContentType = "application/pdf"; //Response.ContentType = @"application/octet-stream"; response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".pdf"); response.OutputStream.Write(pdf, 0, pdf.Length); response.BufferOutput = true; response.BinaryWrite(pdf); response.Flush(); response.End(); }