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