ASP.NET CORE FILE上传问题

编程入门 行业动态 更新时间:2024-10-15 18:30:16
本文介绍了ASP.NET CORE FILE上传问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个可用于chrome的文件上传代码,但会为IE引发以下错误。

$ b

IOException:进程无法访问文件'path\filename'因为它正在被另一个进程使用 System.IO.__ Error.WinIOError(int errorCode,string maybeFullPath) System.IO.FileStream.Init(string path, FileMode模式,FileAccess访问权限,int权限,bool useRights,FileShare共享,int bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,bool bFromProxy,bool useLongPath,bool checkHost) System.IO.FileStream..ctor(string文件模式模式,FileAccess访问,FileShare共享) System.IO.File.OpenWrite(字符串路径) RES.Controllers.DataHandling.DataUploadController + d__5.MoveNext()in DataUploadController.cs $ /

以下控制器操作方法:

public async Task< IActionResult>上传(UploadedData数据) { var filename = string.Empty; if(ModelState.IsValid) { var file = data.File; var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition); filename = Path.Combine(_hostingEnvironment.ContentRootPath,UplaodedFiles,parsedContentDisposition.FileName.Trim(''')); using(var stream = System.IO.File.OpenWrite(filename)) { await file.CopyToAsync(stream); } } }

查看以下模型:

public class UploadedData { public IFormFile File {get; set;} }

我可以想象,代码没有问题,因为它适用于Chrome。有谁知道IE的问题是什么?

解决方案

这是由于Internet Explorer和Chrome之间的不同行为造成的由于安全原因,Chrome隐藏了用户目录的路径。 这意味着你只能得到变量中的文件名(例如 myfile.txt )。

尽管Internet Explorer为您提供了上传文件的完整客户端路径( C:\Users\User1\myfile.txt )。

Path.Combine 失败,然后返回客户端路径(例如 C:\ Users \User1\myfile.txt ),而不是上传的组合路径目录。

您的服务器无法打开文件,因为您的测试/ dev环境中的文件是由您的浏览器打开的。

filename = Path.Combine(@D:\,UploadedFiles,Path.GetFileName(parsedContentDisposition.FileName.Trim(''') ));

具有 Path.GetFileName(..)围绕 parsedContentDisposition.FileName 修复了这个问题。

总之:在IE中试图结合 D:\ , UploadedFiles 和 C:\Users\User1\\ myfile.txt 导致 C:\Users\User1\myfile.txt 并失败。

在Chrome中尝试将code> D:\ , UploadedFiles 和 myfile.txt 导致 D:\UploadedFiles\myfile.txt 我认为这种行为是由于Chrome和Firefox中的这种安全机制,在 Stack Overflow上的这个问题。

strong> Internet Explorer仅上载本地Intranet区域内标准设置的完整路径。

I have a file upload code that works for chrome but throws the following error for IE.

"IOException: The process cannot access the file 'path\filename' because it is being used by another process. System.IO.__Error.WinIOError(int errorCode, string maybeFullPath) System.IO.FileStream.Init(string path, FileMode mode, FileAccess access, int rights, bool useRights, FileShare share, int bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, string msgPath, bool bFromProxy, bool useLongPath, bool checkHost) System.IO.FileStream..ctor(string path, FileMode mode, FileAccess access, FileShare share) System.IO.File.OpenWrite(string path) RES.Controllers.DataHandling.DataUploadController+d__5.MoveNext() in DataUploadController.cs +

Controller action method below:

public async Task<IActionResult> Upload(UploadedData data) { var filename=string.Empty; if (ModelState.IsValid) { var file = data.File; var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition); filename = Path.Combine(_hostingEnvironment.ContentRootPath, "UplaodedFiles", parsedContentDisposition.FileName.Trim('"')); using (var stream = System.IO.File.OpenWrite(filename)) { await file.CopyToAsync(stream); } } }

View model below:

public class UploadedData { public IFormFile File { get; set; } }

I can imagine that there is nothing wrong with the code since it works for Chrome. Does anyone have any idea what the problem is with IE?

解决方案

This is due to different behaviours between Internet Explorer and Chrome. Chrome hides the path to the user directory for security reasons. This means you only get the filename in your variable (e.g. myfile.txt).

While Internet Explorer gives you the full client path of the uploaded file (C:\Users\User1\myfile.txt).

Path.Combine fails then and returns the client path (e.g. C:\Users\User1\myfile.txt) instead of the combined path to your upload directory.

Your server can't open the file since in your test/dev environment the file is opened by your browser.

filename = Path.Combine(@"D:\", "UploadedFiles", Path.GetFileName(parsedContentDisposition.FileName.Trim('"')));

having Path.GetFileName(..) around parsedContentDisposition.FileName fixes that problem.

In short:

Path.Combine in IE tries to combine D:\, UploadedFiles and C:\Users\User1\myfile.txt which results in C:\Users\User1\myfile.txt and fails.

Path.Combine in Chrome tries to combine D:\, UploadedFiles and myfile.txt which results in D:\UploadedFiles\myfile.txt and suceeds.

I think this behaviour is due to this security mechanism in Chrome and Firefox, discussed on this question on Stack Overflow.

Edit: Internet Explorer only uploads the full path on standard settings within the Local Intranet zone.

更多推荐

ASP.NET CORE FILE上传问题

本文发布于:2023-11-17 00:09:41,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传   NET   ASP   FILE   CORE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!