从S3下载后移动文件

问题描述

无法将文件从AWS S3下载到Downloads文件夹。但是,文件已从AWS S3服务器下载到中间本地服务器(E:\ New ** \ E *** \ wwwroot \ DownloadFiles)。现在,问题是将此文件从(\ DownloadFiles)此文件夹获取到Downloads文件夹。尝试这样做时会弹出错误消息。

return File(memory,tmpConType,Path.GetFileName(filePath));

错误弹出窗口

Error-Popup-fromBrowser

这是我下载的完整代码:

[HttpPost]
    public async Task<ActionResult> DownloadFile(string fileName,string originalFilename,int clientId,int taskId)
    {
        using (LogContext.PushProperty("UserId",userId))
        {
            try
            {
                if (fileName != null && fileName != "")
                {
                    var fName = fileName.Split("_")[0];
                    var s3Client = new AmazonS3Client(accesskey,secretkey,bucketRegion);
                    var s3companyFolder = "Company-" + companyId + "/" + "Client-" + clientId + "/" + "Job-" + taskId;

                    GetObjectRequest request = new GetObjectRequest
                    {
                        BucketName = bucketName + "/" + s3companyFolder,Key = fName
                    };

                    using (GetObjectResponse response = await s3Client.GetObjectAsync(request))
                    {
                        using Stream responseStream = response.ResponseStream;
                        string tmpName = response.Metadata["x-amz-meta-filename"];
                        var tmpConType = response.Headers["Content-Type"];

                        
                        string filePath = Path.Combine(hostingEnvironment.WebRootPath,"DownloadFiles");
                        filePath = Path.Combine(filePath,tmpName);
                        using FileStream outFile = System.IO.File.Create(filePath);
                        responseStream.CopyTo(outFile);
                        outFile.Dispose();
                        responseStream.Dispose();

                        var memory = new MemoryStream();
                        using (var stream = new FileStream(filePath,FileMode.Open))
                        {
                            await stream.CopyToAsync(memory);
                        }

                        if (System.IO.File.Exists(filePath))
                            System.IO.File.Delete(filePath);

                        memory.Position = 0;
                        return File(memory,Path.GetFileName(filePath));
                    }
                }
                return Json(new { success = true });
            }
            catch (Exception ex)
            {
               
                throw;
            }
        }
    }

预先感谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...