Home > Backend Development > C++ > How to Stream Files from URLs in ASP.NET?

How to Stream Files from URLs in ASP.NET?

Linda Hamilton
Release: 2025-01-06 08:19:40
Original
559 people have browsed it

How to Stream Files from URLs in ASP.NET?

Downloading and Streaming Files from URLs in ASP.NET

In ASP.NET, streaming files that prompt save prompts in the browser can be achieved using Server.MapPath. However, when the file is located in a virtually mapped directory, this method may not work. Here's an alternative solution using HttpWebRequest to stream files from URLs:

//Create a stream for the file
Stream stream = null;

//This controls how many bytes to read at a time and send to the client
int bytesToRead = 10000;

// Buffer to read bytes in chunk size specified above
byte[] buffer = new Byte[bytesToRead];

// The number of bytes read
try
{
  //Create a WebRequest to get the file
  HttpWebRequest fileReq = (HttpWebRequest) HttpWebRequest.Create(url);

  //Create a response for this request
  HttpWebResponse fileResp = (HttpWebResponse) fileReq.GetResponse();

  if (fileReq.ContentLength > 0)
    fileResp.ContentLength = fileReq.ContentLength;

    //Get the Stream returned from the response
    stream = fileResp.GetResponseStream();

    // prepare the response to the client. resp is the client Response
    var resp = HttpContext.Current.Response;

    //Indicate the type of data being sent
    resp.ContentType = MediaTypeNames.Application.Octet;

    //Name the file
    resp.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

    int length;
    do
    {
        // Verify that the client is connected.
        if (resp.IsClientConnected)
        {
            // Read data into the buffer.
            length = stream.Read(buffer, 0, bytesToRead);

            // and write it out to the response's output stream
            resp.OutputStream.Write(buffer, 0, length);

            // Flush the data
            resp.Flush();

            //Clear the buffer
            buffer = new Byte[bytesToRead];
        }
        else
        {
            // cancel the download if client has disconnected
            length = -1;
        }
    } while (length > 0); //Repeat until no data is read
}
finally
{
    if (stream != null)
    {
        //Close the input stream
        stream.Close();
    }
}
Copy after login

This code allows you to specify a URL for the file, which can be located anywhere, regardless of the physical location of the website. It sets up the appropriate headers and streams the file to the client in chunks, providing a seamless download experience.

The above is the detailed content of How to Stream Files from URLs in ASP.NET?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template