Community discussion forum

Save a Stream to a File

This is a comment thread discussing Save a Stream to a File
  • 10 years ago

    This thread is for discussions of Save a Stream to a File.

  • 1 year ago

    Thank's a lot from Mexico City

  • 9 months ago
    When I pass Response.OutputStream as a readStream parameter to this method it is giving "Specified method is not supported." error in readStream.Read() method. I was trying to save a Response.OutputStream to a file. Please help.
  • 5 months ago

    Here's an example that's optimized for getting copying a file from an ASP.NET FileUpload control and keeping it's path.

    string filePath = uploadFile(fileUploadControl.FileContent);
    
    private string uploadFile(Stream serverFileStream)
    {
        string filename = ConfigurationManager.AppSettings["FileUploadTempDir"] + 
            DateTime.Now.ToString("yyyyMMddhhmm") + "_" + 
            Customer.GetCustomerName(CustomerId).Replace(" ", "_") + ".txt";
    
        try
        {
            int length = 256;
            int bytesRead = 0;
            Byte[] buffer = new Byte[length];
    
            // write the required bytes
            using (FileStream fs = new FileStream(filename, FileMode.Create))
            {
                do
                {
                    bytesRead = serverFileStream.Read(buffer, 0, length);
                    fs.Write(buffer, 0, bytesRead);
                }
                while (bytesRead == length);
            }
    
            serverFileStream.Dispose();
            return filename;
        }
        catch (Exception ex)
        {
            lblErrorMessage.Text += "An unexpeded error occured uploading the file. " + ex.Message;
            return string.Empty;
        }
    
    }
    

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!