如何在 .netcore 中关闭文件流

尼拉吉

我是 .net core 和 c# 的新手。我的项目中有文件上传要求。我正在尝试将我的文件上传到 aws s3。我从项目根文件夹中的文件夹上传文件,然后从那里删除上传的文件。但是当我尝试上传到 s3 时出现以下错误

The process cannot access the file because it is being used by another process.

这是我上传文件的代码

string fileFolder = Path.Combine(hostingEnvironment.WebRootPath, "TempFiles");
                    uniqueFileName1 = Guid.NewGuid().ToString() + "_" + cm.UDocument1.FileName;
                    string filePath = Path.Combine(fileFolder, uniqueFileName1);
                    cm.UDocument1.CopyTo(new FileStream(filePath, FileMode.Create));

                    var tempPath = Path.Combine(hostingEnvironment.WebRootPath, "TempFiles", Path.GetFileName(uniqueFileName1));
                    UploadFile(cm.UDocument1,tempPath);

[HttpPost]
    public ActionResult UploadFile(IFormFile file,string path)
    {

        var s3Client = new AmazonS3Client(accesskey, secretkey, bucketRegion);

        var fileTransferUtility = new TransferUtility(s3Client);
        try
        {
            if (file.Length > 0)
            {

                var fileTransferUtilityRequest = new TransferUtilityUploadRequest
                {
                    BucketName = bucketName,
                    FilePath = path,
                    StorageClass = S3StorageClass.Standard,
                    Key = file.FileName
                };
                fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
                fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
                fileTransferUtility.Upload(fileTransferUtilityRequest);
                fileTransferUtility.Dispose();

                string[] tmp = { "" };
                tmp = fileName.Split("-");

            }
            ViewBag.Message = "File Uploaded Successfully!!";

            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }
        }

        return ViewBag.Message;

    }

我该怎么办?

乔希

在这一行:

cm.UDocument1.CopyTo(new FileStream(filePath, FileMode.Create));

您创建了一个文件流,但没有处理它。

改为使用 using 语句换行:

using (var iNeedToLearnAboutDispose = new FileStream(filePath, FileMode.Create))
{
    cm.UDocument1.CopyTo(iNeedToLearnAboutDispose);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在NETCore中播放声音?

如何在.NETCore 3.1和Blazor中创建DbContextFactory

如何在 TFS 发布管道中运行 NETCore 集成测试?

我如何在ClaimsIdentity Asp.NetCore中传递对象

如何在 Rider IDE 中更改 SDK 版本 asp netcore?

如何解决 .netcore 中的 FileLoadException

如何在asp.netcore中解析JSON中动态变化的响应对象

如何在.netcore 1.1中使用动态类型循环Json?

如何在netcore中间件中获得正确的响应体?

如何在UseMvc中间件外部的.NetCore 3.1中注册OData扩展方法

如何在.NetCore 2.0中的WebAPI上的Azure中使用应用程序设置

如何在.netcore 2 C#中获取地理位置

如何在.netcore3.0 web api中编写aync方法

如何在 .netcore 3.0 中以 IAsyncEnumerable<T> 作为返回类型拦截异步方法

如何在ASP .NetCore MVC Web应用程序中设置视频背景

如何在 Asp.NetCore 控制器中模拟 HttpResponse?

grails应用程序中的“打开的文件太多”-如何正确关闭打开的文件和流?

如何在 Java 中关闭嵌套的 I/O 流

如何在Java中关闭PST文件?

如何在终端中关闭文件

流结束后如何关闭文件?

在使用.netcore的razor Web应用程序中,如何在表单发布请求完成后从页面后面的代码关闭Web浏览器窗口

如何多次读取netcore 3中的http请求正文?

如何从.NetCore 2.2中的Startup注入2 EventHubClient

如何正确重定向到asp.netcore中的页面

如何从Lambda向.NetCore中的SQS发送kinesisEvent故障

如何解析.netcore类库中的JSON

如何在.netcore中的twilio短信“接收并回复入站短信”中捕获发件人号码?

Ruby:如何在脚本中调用文件后关闭文件