开发者

How do I stream binary data to disk in asp.net with c#

开发者 https://www.devze.com 2023-04-07 18:03 出处:网络
How do I stream binary data to disk, where the user first choose the location path? What I开发者_StackOverflow have so far:

How do I stream binary data to disk, where the user first choose the location path?

What I开发者_StackOverflow have so far: User click in my RadGrid, and I fetch Binary (or byte[] with .ToArrar()).

I would like something, where user was ask to browse he's computer for at location and hitting accept/cancel. And accepting would start the stream to write the file.


Basically you set the response object to oclet type, push in the data and send it off. The client browser determines how it will display any needed dialogs to the user.

This is from a download utility page on an internal web app. The full code includes protection against the user trying to read files outside of its path sandbox that I ommited for this example.

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";
    }
}


You don't (can't) stream data directly to the user's disk or interact outside of the user's browser. In a web application all you need to do is deliver the content to the user as a standard HTTP response. The user's browser will take care of the rest.

There's a really good question/answer about this here.

Understand that the HTTP protocol doesn't deal in "files." It deals in requests and responses, each of which consists of headers and a body. So what your web application would do is craft a response which the user's browser might interpret as something that it should save as a file. The headers would provide the browser with what it needs to make this interpretation, and the body would provide the browser with the data. Generally it involves these steps:

  1. Remove any existing output (don't send page markup or anything like that).
  2. Set your headers accordingly. In this case you'll want to set things like content-length, content-type, and probably content-disposition as a way of suggesting to the browser that it save the response as a file.
  3. Write the bytes of the file to the response stream.
  4. End the response stream.
0

精彩评论

暂无评论...
验证码 换一张
取 消