How can I upload a file and save it to a Stream for further preview using C#?

Sure is. I upload files (PDF/images) to my db in my app. My model object actually stores the file as a byte array but for other functions i have to convert to and from streams so im sure its just as easy to keep it in stream format.

Here are some code examples (copy n paste) from my app-

The File object that i use to move files (PDFs / images) around:

public class File : CustomValidation, IModelBusinessObject
{
    public int ID { get; set; }
    public string MimeType { get; set; }
    public byte[] Data { get; set; }
    public int Length { get; set; }
    public string MD5Hash { get; set; }
    public string UploadFileName { get; set; }
}

..the PdfDoc type specifically for PDF files:

public class PdfDoc : File
{
    public int ID { get; set; }
    public int FileID
    {
        get { return base.ID; }
        set { base.ID = value; }
    }
    [StringLength(200, ErrorMessage = "The Link Text must not be longer than 200 characters")]
    public string LinkText { get; set; }


    public PdfDoc() { }

    public PdfDoc(File file)
    {
        MimeType = file.MimeType;
        Data = file.Data;
        Length = file.Length;
        MD5Hash = file.MD5Hash;
        UploadFileName = file.UploadFileName;
    }

    public PdfDoc(File file, string linkText)
    {
        MimeType = file.MimeType;
        Data = file.Data;
        Length = file.Length;
        MD5Hash = file.MD5Hash;
        UploadFileName = file.UploadFileName;

        LinkText = linkText;
    }
}

.. an example of an action that receives multi-part POST for file uploading:

    //
    // POST: /Announcements/UploadPdfToAnnouncement/ID
    [KsisAuthorize(Roles = "Admin, Announcements")]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult UploadPdfToAnnouncement(int ID)
    {
        FileManagerController.FileUploadResultDTO files =
            FileManagerController.GetFilesFromRequest((HttpContextWrapper)HttpContext);
        if (String.IsNullOrEmpty(files.ErrorMessage) && files.TotalBytes > 0)
        {
            // add SINGLE file to the announcement
            try
            {
                this._svc.AddAnnouncementPdfDoc(
                    this._svc.GetAnnouncementByID(ID),
                    new PdfDoc(files.Files[0]),
                    new User() { UserName = User.Identity.Name });
            }
            catch (ServiceExceptions.KsisServiceException ex)
            {
                // only handle our exceptions
                base.AddErrorMessageLine(ex.Message);
            }
        }

        // redirect back to detail page
        return RedirectToAction("Detail", "Announcements", new { id = ID });
    }

Now you can see i pass the file object to my service here but you can choose to add it to the session and pass an id back to the ‘preview’ view for example.

Finally, here is a generic action i use to render files out to the client (you could have something similar render the files/stream from the Session):

    //
    // GET: /FileManager/GetFile/ID
    [OutputCache(Order = 2, Duration = 600, VaryByParam = "ID")]
    public ActionResult GetFile(int ID)
    {
        FileService svc = ObjectFactory.GetInstance<FileService>();

        KsisOnline.Data.File result = svc.GetFileByID(ID);

        return File(result.Data, result.MimeType, result.UploadFileName);
    }

EDIT:
I noticed i need more samples to explain the above-

For the upload action above, the FileUploadResultDTO class:

    public class FileUploadResultDTO
    {
        public List<File> Files { get; set; }
        public Int32 TotalBytes { get; set; }
        public string ErrorMessage { get; set; }
    }

And the GetFilesFromRequest function:

    public static FileUploadResultDTO GetFilesFromRequest(HttpContextWrapper contextWrapper)
    {
        FileUploadResultDTO result = new FileUploadResultDTO();
        result.Files = new List<File>();

        foreach (string file in contextWrapper.Request.Files)
        {
            HttpPostedFileBase hpf = contextWrapper.Request.Files[file] as HttpPostedFileBase;
            if (hpf.ContentLength > 0)
            {
                File tempFile = new File()
                {
                    UploadFileName = Regex.Match(hpf.FileName, @"(/|\\)?(?<fileName>[^(/|\\)]+)$").Groups["fileName"].ToString(),   // to trim off whole path from browsers like IE
                    MimeType = hpf.ContentType,
                    Data = FileService.ReadFully(hpf.InputStream, 0),
                    Length = (int)hpf.InputStream.Length
                };
                result.Files.Add(tempFile);
                result.TotalBytes += tempFile.Length;
            }
        }

        return result;
    }

And finally (i hope i have everything you need now) this ReadFully function. It’s not my design. I got it from the net – stream reading can be tricky. I find this function is the most successful way to completely read a stream:

    /// <summary>
    /// Reads data from a stream until the end is reached. The
    /// data is returned as a byte array. An IOException is
    /// thrown if any of the underlying IO calls fail.
    /// </summary>
    /// <param name="stream">The stream to read data from</param>
    /// <param name="initialLength">The initial buffer length</param>
    public static byte[] ReadFully(System.IO.Stream stream, long initialLength)
    {
        // reset pointer just in case
        stream.Seek(0, System.IO.SeekOrigin.Begin);

        // If we've been passed an unhelpful initial length, just
        // use 32K.
        if (initialLength < 1)
        {
            initialLength = 32768;
        }

        byte[] buffer = new byte[initialLength];
        int read = 0;

        int chunk;
        while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
        {
            read += chunk;

            // If we've reached the end of our buffer, check to see if there's
            // any more information
            if (read == buffer.Length)
            {
                int nextByte = stream.ReadByte();

                // End of stream? If so, we're done
                if (nextByte == -1)
                {
                    return buffer;
                }

                // Nope. Resize the buffer, put in the byte we've just
                // read, and continue
                byte[] newBuffer = new byte[buffer.Length * 2];
                Array.Copy(buffer, newBuffer, buffer.Length);
                newBuffer[read] = (byte)nextByte;
                buffer = newBuffer;
                read++;
            }
        }
        // Buffer is now too big. Shrink it.
        byte[] ret = new byte[read];
        Array.Copy(buffer, ret, read);
        return ret;
    }

Leave a Comment