asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-mvc-3 – MVC HttpPostedFileBase总是空大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要一些帮助。我正在尝试使用< input type =“file”>上传文件。这里是我的视图:
@using (Html.beginForm("BookAdd","Admin",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
    <input type="file" name="files[0]" id="files[0]" />
    <input type="file" name="files[1]" id="files[1]" />
    <input type="submit" value="Upload Book" />
}

这里是一个动作,应该处理上传的文件。

[httpPost]
public ActionResult BookAdd(IEnumerable<httpPostedFileBase> files)
{
    // some actions
    return View();
}

问题是“文件”总是包含两个为null的元素。
可以做什么来解决它?

是时候了一些新闻。似乎我发现了问题,但我还是不知道如何解决它。看来,尽管事实上我在这里使用“R_751_11845@ultipart / form-data”:

@using (Html.beginForm("BookAdd",new { enctype="multipart/form-data" }))
{
    <input type="file" name="File" id="file1" />
    <input type="file" name="File" id="file2" />
    <input type="submit" value="Upload Book" />
}

request.ContentType在控制器中保持“application / x-www-forum-urlencoded”。

解决方法

只需删除输入字段名称中的方括号:
@using (Html.beginForm("BookAdd",new { enctype = "multipart/form-data" }))
{
    <input type="file" name="files" id="file1" />
    <input type="file" name="files" id="file2" />
    <input type="submit" value="Upload Book" />
}

更新:

看看你发送给我的示例项目的问题是,你有2个嵌套的表单。这在HTML中不允许。在您的_Layout.cshtml中有一个表单,在BookAdd.cshtml视图中有另一个表单。这就是为什么尽管在内部形式的enctype =“multipart / form-data”属性,你得到错误的request.ContentType。所以如果你想要这个工作,你将不得不排除这些形式。另外在示例中,你发送给我的BookAdd控制器动作没有正确的签名采取文件列表,但我想这是由于一些测试你在做。

大佬总结

以上是大佬教程为你收集整理的asp.net-mvc-3 – MVC HttpPostedFileBase总是空全部内容,希望文章能够帮你解决asp.net-mvc-3 – MVC HttpPostedFileBase总是空所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。