2015-07-29 5 views
0

я пытаюсь загрузить файл из формы с помощью ASP MVC 5.ASP .NET файл загрузки - HttpPostedFileBase = нуль

Но мой 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> 

} 

ответ

3

Используйте это:

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 .... 
    } 
} 

Snapshot:

Working example

+0

Спасибо, мой запрос есть ContentLength> 0 (Request.File [0]), butmy вар файл = Request.Files [0] имеют ContentLength = 0 – Alexis

+0

О, это, наконец, работа, моя contentLength автоматически переходит в 0 в моей отладке, но моя вставка в DB выглядит хорошо. Спасибо ! – Alexis

+0

Наконец-то вы получили решение, довольный этим. –

Смежные вопросы