简体   繁体   English

在内存中创建FileStream而不是在磁盘上保存物理文件

[英]Create FileStream in memory instead of saving a physical file on disk

I am uploading a file to an api, and I have to copy my requestStream to a FileStream in order to post the file to the API. 我正在将文件上传到api,并且必须将我的requestStream复制到FileStream才能将文件发布到API。 My code below works, but I have to save the file to a temp folder to create the FileStream, and then I have to clean up the temp folder again after the operation. 下面的代码可以工作,但是我必须将文件保存到temp文件夹中以创建FileStream,然后在操作后必须再次清理temp文件夹。 Is there a cleaner way of doing that - eg creating the FileStream in memory (if that's possible) instead of saving it to the disk? 有没有一种更清洁的方法-例如在内存中创建FileStream(如果可能的话)而不是将其保存到磁盘上?

Stream requestStream = await Request.Content.ReadAsStreamAsync();

     //Create filestream by making a temporary physical file
     using (FileStream fileStream = System.IO.File.Create(@"C:\tempFolder\" fileName))                                     
     {
         await requestStream.CopyToAsync(fileStream);
         var postedFile = ms.CreateMedia(fileName, folder.Id, "file");                                    
         postedFile.SetValue("umbracoFile", fileName, fileStream);                                      
         ms.Save(postedFile);
     }

     // Clean up
     if ((System.IO.File.Exists(@"C:\tempFolder\" + fileName)))
     {
         System.IO.File.Delete(@"C:\tempFolder\" + fileName);
     }

Why not use the requestStream directly? 为什么不直接使用requestStream It is an instance of Stream and your API end point expects an instance of Stream , there is no need to copy the content of requestStream to any intermediary point unless you want to add unnecessary overhead. 它是Stream一个实例,并且您的API端点需要一个Stream的实例,除非您想要添加不必要的开销,否则无需将requestStream的内容复制到任何中间点。

Stream requestStream = await Request.Content.ReadAsStreamAsync();

var postedFile = ms.CreateMedia(fileName, folder.Id, "file");
postedFile.SetValue("umbracoFile", fileName, requestStream);
ms.Save(postedFile);

.Net doesn't really provide a good in-memory solution. .Net并未真正提供良好的内存解决方案。 It is good to create your own to save resources and encapsulate more functionality. 最好自己创建一个,以节省资源并封装更多功能。

Check out this guide here 在这里查看本指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在内存而不是物理文件中创建PDF - Create PDF in memory instead of physical file 在 memory 中创建 eml 文件而不将其保存在磁盘上 - Create eml file in memory without saving it on disk iText7 在内存中创建 PDF 而不是物理文件 - iText7 Create PDF in memory instead of physical file 将文件转换保存为变量而不是物理位置 - Saving File conversion to variable instead of physical location 将文件保存在内存中,而不是物理路径 - save a file in memory instead of a physical path 使用ABCPDF在内存中生成文档,而不是保存在物理驱动器上 - Using ABCPDF to generate document in memory instead of saving on physical drive 写入/还原二进制文件流时,保存文件的创建/修改日期 - Saving create / modify dates of file when writing / restoring a binary filestream 将压缩的内存流保存到文件流,最终文件已损坏 - Saving compressed memory stream to filestream , endfile is corrupted 立即下载新创建的Word文件,而不是保存到磁盘 - Download newly created word file straight away instead of saving to disk API中的NPOI是否可以在不保存到磁盘的情况下使用内存中的Excel文件? - Can NPOI in API use Excel file in memory without saving to disk?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM