Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在mvc中上载文件_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 无法在mvc中上载文件

C# 无法在mvc中上载文件,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我第一次在web服务器上上传MVC网站。除了文件上传选项外,一切正常。每当我尝试上载文件时,它都会提示我以下错误: SaveAs方法配置为需要根路径,并且 “”不是根目录 错误详细信息以某种方式显示了我在本地计算机上创建的项目的路径 System.Web.HttpException(0x80004005):已配置SaveAs方法 需要根路径,而路径“”不是根路径。在 Printrpk.Controllers.ProductController.Create(ProductModels产品) 在里

我第一次在web服务器上上传MVC网站。除了文件上传选项外,一切正常。每当我尝试上载文件时,它都会提示我以下错误:

SaveAs方法配置为需要根路径,并且 “”不是根目录

错误详细信息以某种方式显示了我在本地计算机上创建的项目的路径

System.Web.HttpException(0x80004005):已配置SaveAs方法 需要根路径,而路径“”不是根路径。在 Printrpk.Controllers.ProductController.Create(ProductModels产品) 在里面 C:\Projects\Carting\Carting\Carting\Controllers\ProductController.cs:line 145

我尝试了多种方法来检查我的代码是否错误,但我无法使其正常工作

这是我在创建操作下写的内容

foreach (string fileName in Request.Files)
{
    HttpPostedFileBase file = Request.Files[fileName];
    fName = file.FileName;
    if (file != null && file.ContentLength > 0)
    {
        var originalDirectory = new DirectoryInfo(string.Format("{0}images", Server.MapPath(@"/")));
        var pathString = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ProductImages");
        var filename = Path.GetFileName(file.FileName);

        bool isExists = System.IO.Directory.Exists(pathString);
        if (!isExists)
        {
            System.IO.Directory.CreateDirectory(pathString);
        }

        path = string.Format("{0}//{1}", pathString, filename);
        file.SaveAs(path);
    }
}
当我不上传任何图像时,该控制器工作正常。我已经检查了我的web.config,它已经指向web服务器

这包括在
Create
视图中

<input name="file" type="file" multiple />

此外,我的基本文件夹中已经有目标文件夹

使用
Server.MapPath()
获取与虚拟路径对应的物理路径。假设您的应用程序包含名为
Images
的文件夹,其中包含名为
ProductImages
的子文件夹,则

var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Images/ProductImages"), fileName);
file.SaveAs(path);

边注:考虑将参数<代码> iQuestabl文件< /代码>添加到POST方法中,以便它与所选文件绑定(或者更好,包含在视图模型中的属性),而不是使用<代码>请求.file < /COD> < /P> < P>使用<代码> Server .MaMPATAL()>代码>以获得对应于虚拟路径的物理路径。假设您的应用程序包含名为

Images
的文件夹,其中包含名为
ProductImages
的子文件夹,则

var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Images/ProductImages"), fileName);
file.SaveAs(path);

边注:考虑将参数<代码> iQuestabl文件< /代码>添加到POST方法中,以便它与所选文件绑定(或更好,包含在视图模型中的属性),而不是使用<代码>请求.file < /COD> < /P>您检查AppDima.CurrrdModay.Basic目录值吗?使用<代码> Server .MaPASTAN()-例如,

var fileName=Path.GetFileName(file.fileName);var path=path.Combine(Server.MapPath(“~/ProductImages”),文件名);file.SaveAs(路径)@StephenMuecke我已经试过了。不起作用。你的应用程序是否包含文件夹
ProductImages
?当你使用它时,
path
的值是多少?谢谢@StephenMuecke,我明白了。你的代码现在可以工作了。我插入了错误的变量。请在下面写下你的答案,以便我投票:)你检查过AppDomain.CurrentDomain.BaseDirectory值吗?使用
Server.MapPath()
-例如
var fileName=Path.GetFileName(file.fileName);var path=path.Combine(Server.MapPath(“~/ProductImages”),文件名);file.SaveAs(路径)@StephenMuecke我已经试过了。不起作用。你的应用程序是否包含文件夹
ProductImages
?当你使用它时,
path
的值是多少?谢谢@StephenMuecke,我明白了。你的代码现在可以工作了。我插入了错误的变量。请把你的答案写在下面,这样我就可以投票了:)