【问题标题】:ASP .NET upload file - HttpPostedFileBase = nullASP .NET 上传文件 - HttpPostedFileBase = null
【发布时间】:2015-10-20 00:51:08
【问题描述】:

我正在尝试使用 ASP MVC 5 从表单上传文件。

但我的file.ContentLength 总是 = 0

public ActionResult Test(int? id, HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0)
    {
       //blabla
    }
}

这里是我的 .cshtml 表单

using (Html.BeginForm("Test", "MyController",FormMethod.Post, new { id = Model.Id, enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

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

    <div class="form-group">
        <button type="submit">Ajouter</button>
    </div>

}

【问题讨论】:

    标签: c# asp.net razor file-upload


    【解决方案1】:

    使用这个:

    var count = Request.Files.Count;
    
    if (count > 0) {
       var files = Request.Files[0];
       if(files.ContentLength > 0){
           Your work here e.g : string name = files.FileName ....
       }
    }
    

    快照:

    【讨论】:

    • 谢谢,我的请求有一个 contentLength>0 (Request.File[0]),但是我的 var file = Request.Files[0] 有一个 contentLength=0
    • 哦,它终于起作用了,我的 contentLength 在我的调试中自动变为 0,但我在 DB 中的插入看起来不错。谢谢!
    • 终于找到了解决方案,为此感到高兴。
    猜你喜欢
    • 2012-06-07
    • 1970-01-01
    • 2013-11-11
    • 2019-07-17
    • 2014-06-29
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多