Get File. Stream Using Path

I would like to get the File and pass it as stream but I'm not sure how to do this. I do have a path where the file is located. So basically using this path I'm going to retrieve this file and save it as stream. But correct me if I'm wrong, but is it correct to assume that "Reading as Stream" is something like it's actually opening the file and reading it?

Are there anyway to just retrieve or Copy the file into a FileStream or MemoryStream? Right now I have this but not sure if this is correct

 foreach (string filePath in filePaths)
 {
      string filename = Path.GetFileName(filePath);
      //Constants.Conventions.MediaTypes.Image
      string mediaType = Constants.Conventions.MediaTypes.File;
      string ext = Path.GetExtension(filename);
      IMedia media = Services.MediaService.CreateMedia(filename, Constants.System.Root, mediaType);
      MemoryStream ms = new MemoryStream();
      using (Stream stream = System.IO.File.OpenRead(filePath))
      {
           ms.WriteTo(stream);
      }
      media.SetValue("umbracoFile", filename, ms);
      // Save the media
      Services.MediaService.Save(media);
      media = null;
      System.IO.File.Delete(filePath);
 }

I'm not so sure if this is the best approach. But the main goal is to retrieve that file from the path and save it in media.SetValue("umbracoFile", filename, ms); as it's requiring a Stream as input. How do I do this in a most efficient way? I'm always getting outofmemoryexception due to large file. So I'm guessing because it's reading it as Stream that it's throwing an error on large file. So maybe we can just copy it to save. I'm not so sure really needs help here

Update: Here is the config to make sure I don't run out of memory

<httpRuntime requestValidationMode="2.0" enableVersionHeader="false" maxRequestLength="2097151" executionTimeout="50000000" targetFramework="4.5" fcnMode="Single" />

<location path="umbraco">
  <system.web>
    <httpRuntime maxRequestLength="204800" executionTimeout="99999"/>
  </system.web>
</location>

<security>
  <requestFiltering>
    <requestLimits maxAllowedContentLength="2294967295" />
  </requestFiltering>
</security>

And this is the build of my project

7

2 Answers

You could try something like the code below and save yourself the trouble of opening it and then converting to a memory stream by using FileStream.

foreach (string filePath in filePaths)
{
    string filename = Path.GetFileName(filePath);
    string mediaType = Constants.Conventions.MediaTypes.File;
    string ext = Path.GetExtension(filename);
    IMedia media = Services.MediaService.CreateMedia(filename, Constants.System.Root, mediaType);
    using (var fs = new FileStream(filePath, FileMode.Open))
    {
        media.SetValue("umbracoFile", filename, fs);
        Services.MediaService.Save(media);
    }

    media = null;
    System.IO.File.Delete(filePath);
}

You can simply copy FileStream to MemoryStream in .Net Framework 4+

MemoryStream ms = new MemoryStream();
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    file.CopyTo(ms);
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest