Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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# 在asp.net mvc3中上载文件_C#_Asp.net Mvc_Asp.net Mvc 3_Razor - Fatal编程技术网

C# 在asp.net mvc3中上载文件

C# 在asp.net mvc3中上载文件,c#,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,我需要上传一个文件,我找到了几种方法。我认为最好的方法是根据这个博客: 我还发现了一个有用的帖子: 但当我尝试时,它失败了。我在visual studio中遇到以下错误: 序列包含不止一个元素,没有更多的元素可以继续 我的代码如下所示: @using (Ajax.BeginForm("Method", "Controller",new { enctype = "multipart/form-data" }, new AjaxOptions { HttpMethod = "Post", OnS

我需要上传一个文件,我找到了几种方法。我认为最好的方法是根据这个博客:

我还发现了一个有用的帖子:

但当我尝试时,它失败了。我在visual studio中遇到以下错误:

序列包含不止一个元素,没有更多的元素可以继续

我的代码如下所示:

@using (Ajax.BeginForm("Method", "Controller",new { enctype = "multipart/form-data" }, new AjaxOptions { HttpMethod = "Post", OnSuccess = "Method" }, new { @class = "css", id = "formname" }))
{
   Some divs and stuff

   <input type="file" name="file" class="btn btn-inverse" />
   </div>
[HttpPost]
        public void _SaveDocument(Model model, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath(@"Path"), fileName);
                file.SaveAs(path);

                var command = new command()
                {
                    Model = model
                };
                commandDispatcher.Dispatch(command);
            }            
        }
控制器:

public PartialViewResult Index(parameterlist)
    {
        var model = new Model();


        return PartialView(model);
    }

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0) 
        {
            // extract only the filename
            var fileName = Path.GetFileName(file.FileName);

            var path =         Path.Combine(Server.MapPath(@"Path"), fileName);
            file.SaveAs(path);
        }
        // redirect back to the index action to show the form once again
        return RedirectToAction("Index");            
    }
视图:


@使用(Html.BeginForm(“null”、“null”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
}

因此,当调用此partialview时,它将转到actionResult方法,当然,没有选择任何文件,因为视图尚未打开。视图中还有一些其他文本框和下拉列表。但没什么特别的,有人知道我做错了什么吗?我觉得我遗漏了一些重要的东西…

为了避免执行错误的函数,应该在Html.BeginForm中指定控制器和操作,而不是其中的空值

@using (Html.BeginForm("Index", "YourController", FormMethod.Post, ...

为了避免执行错误的函数,应该在Html.BeginForm中指定控制器和操作,而不是其中的空值

@using (Html.BeginForm("Index", "YourController", FormMethod.Post, ...

我尝试将它们合并到一个表单中,因此我的表单现在如下所示:

@using (Ajax.BeginForm("Method", "Controller",new { enctype = "multipart/form-data" }, new AjaxOptions { HttpMethod = "Post", OnSuccess = "Method" }, new { @class = "css", id = "formname" }))
{
   Some divs and stuff

   <input type="file" name="file" class="btn btn-inverse" />
   </div>
[HttpPost]
        public void _SaveDocument(Model model, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath(@"Path"), fileName);
                file.SaveAs(path);

                var command = new command()
                {
                    Model = model
                };
                commandDispatcher.Dispatch(command);
            }            
        }

但文件从未被添加,它总是空的

我尝试将它们合并到一个表单中,因此我的表单现在看起来如下所示:

@using (Ajax.BeginForm("Method", "Controller",new { enctype = "multipart/form-data" }, new AjaxOptions { HttpMethod = "Post", OnSuccess = "Method" }, new { @class = "css", id = "formname" }))
{
   Some divs and stuff

   <input type="file" name="file" class="btn btn-inverse" />
   </div>
[HttpPost]
        public void _SaveDocument(Model model, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath(@"Path"), fileName);
                file.SaveAs(path);

                var command = new command()
                {
                    Model = model
                };
                commandDispatcher.Dispatch(command);
            }            
        }

但是文件永远不会被添加,它总是空的

您可以发布stacktrace或您收到的文本异常消息吗?一切看起来都很好,但是如果您希望避免此实现,您可以使用Microsoft.Web.Helpers命名空间,因为它包含一个可以帮助您的FileUpload类。好的,我找到了为什么会出现这个异常,我需要在重定向到操作(“索引”)时传递参数列表,但是现在页面加载了,但是当我尝试上载一个文件时,它不会上载,而是执行页面上另一个按钮的功能。你能发布stacktrace或你得到的文本异常消息吗?一切看起来都很好,但是,如果希望避免此实现,可以使用Microsoft.Web.Helpers命名空间,因为它包含一个可以帮助您的FileUpload类。好的,我发现了为什么我会出现这个异常,我需要在重定向到操作(“索引”)时传递参数列表,但是现在页面加载了,但是当我尝试上载一个文件时,它不会上载,而是执行页面上另一个按钮的功能。我现在发现,我视图中的问题是,我在表单中有一个表单。我需要提交与其他细节一起上传的图像。但在输入这些细节之前,必须先上传文件,这也意味着要提到这一点。看起来你已经把它们分开并分类了。我现在明白了,在我看来,问题是我有一个表单中的一个表单。我需要提交与其他细节一起上传的图像。但在输入这些细节之前,必须先上传文件,这也意味着要提到这一点。看起来您已经将它们分开并排序了。您无法使用ajax上载文件。要做到这一点,您需要查看一些特殊组件,如Ajax Upload或Uploadify。您不能使用Ajax上传文件。要做到这一点,您需要查看一些特殊的组件,如Ajax Upload或Uploadify。