繁体   English   中英

System.IO.IOException: '进程无法访问文件“文件位置”,因为它正被另一个进程使用。

[英]System.IO.IOException: 'The process cannot access the file "file location"because it is being used by another process.'

我需要保存一个文件然后阅读它。 但我收到错误:

进程无法访问文件”

当我调试它时。

当我通过 Chrome 调试时,我也收到此错误。 但是,当我通过 IE 调试相同的代码时,出现错误:

不支持给定路径的格式”。

不知道出了什么问题

控制器.cs

public HttpResponseMessage Upload()
{
    HttpResponseMessage response = new HttpResponseMessage();
    var httpRequest = HttpContext.Current.Request;
    if (httpRequest.Files.Count > 0)
    {
        foreach (string file in httpRequest.Files)
        {
            var postedFile = httpRequest.Files[file];
            string fileName = Path.GetFileName(postedFile.FileName);
            string fileExtension = Path.GetExtension(postedFile.FileName);
            string fileLocation = HttpContext.Current.Server.MapPath("~/UploadFile/" + postedFile.FileName);
            postedFile.SaveAs(fileLocation);

PostedFile.SaveAs(fileLocation)是引发错误的点。

posted.FileName是客户端计算机上的完整路径,如C:\\users\\myName\\Documents\\mydoc.pdf 您正在尝试将其连接到服务器UploadFile文件夹,然后对该结果字符串调用Server.MapPath

这永远不会奏效。

您已经从完整路径中提取了文件名部分。 所以就用它吧。

string fileLocation = 
   HttpContext.Current.Server.MapPath("~/UploadFile/" + fileName);
if (!(Directory.Exists((fileLocation ))))
                {
                    Directory.CreateDirectory((fileLocation ));
                }
     postedFile.SaveAs(fileLocation);

如果出现错误,请尝试此操作,请发布该变量中的“fileLocation”值。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM