如何将二进制数据流式传输到用户首先选择位置路径的磁盘?
到目前为止我所拥有的:用户在我的RadGrid中单击,然后我获取二进制文件(或使用.ToArrar()获取byte[] )。
我想要的东西,其中用户被要求浏览他的计算机的位置,并点击接受/取消。而接受将启动流以写入文件。
发布于 2011-09-27 21:29:02
基本上,您将响应对象设置为oclet类型,推入并发送数据。客户端浏览器确定它将如何向用户显示任何需要的对话框。
这是来自内部web应用程序的下载实用程序页面。完整的代码包括防止用户尝试读取路径沙箱之外的文件的保护,我在本例中省略了这些保护。
string document = "... some server document file name ...";
string fullpath = Server.MapPath("your path"+document);
Response.ContentType = ExtentionToContentType(document);
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", document));
byte[] data = System.IO.File.ReadAllBytes(fullpath);
Response.AppendHeader("Content-Length", data.Length.ToString());
Response.BinaryWrite(data);
Response.End();
public string ExtentionToContentType(string file)
{
switch (System.IO.Path.GetExtension(file).ToLower())
{
case ".xls": return "application/vnd.ms-excel";
case ".doc": case ".docx": return "application/msword";
case ".ppt": return "application/vnd.ms-powerpoint";
case ".mdb": return "application/x-msaccess";
case ".zip": return "application/zip";
case ".jpg": case ".jpeg": case ".jpe": return "image/jpeg";
case ".tiff": return "image/tiff";
case ".bmp": return "image/bmp";
case ".png": return "image/png";
case ".gif": return "image/gif";
case ".avi": return "video/x-msvideo";
case ".mpeg": return "video/mpeg";
case ".rtf": case ".rtx": return "text/richtext";
case ".txt": return "text/plain";
case ".pdf": return "application/pdf";
default: return "application/x-binary";
}
}发布于 2011-09-27 21:33:13
您不能(不能)将数据直接流式传输到用户的磁盘,也不能在用户浏览器之外进行交互。在web应用程序中,您所需要做的就是将内容作为标准HTTP响应传递给用户。用户的浏览器将会处理剩下的事情。
关于这个here有一个非常好的问题/答案。
理解HTTP协议不处理“文件”。它处理请求和响应,每个请求和响应都由头部和主体组成。因此,您的web应用程序要做的就是制作一个响应,用户的浏览器可能会将其解释为应该保存为文件的内容。头部将为浏览器提供进行这种解释所需的内容,主体将为浏览器提供数据。一般来说,它涉及到以下步骤:
content-length、content-type和content-disposition之类的内容,以便向浏览器建议将响应保存为文件。https://stackoverflow.com/questions/7569900
复制相似问题