1

I want to upload Pdf file into my application

So I have ViewModel (I post only relevant code)

public class SubcategoryViewModel
{
    public HttpPostedFileBase PdfFile { get; set; }
    [DisplayName("PDF")]
    public string Pdf { get; set; }
}

Controller:

 public async Task<string> CreateSubcategory(SubcategoryViewModel model)
    {
              string pdf = null;
            if (model.Pdf != null)
            {
                pdf = Guid.NewGuid().ToString().Substring(0, 13) + "_" + model.File.FileName;
                model.Pdf = pdf;
                var path =    Path.Combine(HttpContext.Current.Server.MapPath("~/Content/pdf"), pdf);
                model.File.SaveAs(path);
            }

            var subcategory = new Subcategory
            {
                Pdf = pdf,
            };
            db.SubcategoriesList.Add(subcategory);
            await db.SaveChangesAsync();

View:

@model Models.ViewModels.SubcategoryViewModel
@using (Html.BeginForm("Create", "Subcategory", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{
<div class="form-group">
        @Html.LabelFor(model => model.Pdf, new { @class = "control-label col-md-2" })
        <div class="col-md-10 legend-size">
            <input type="file" id="file" name="file" />
        </div>
    </div>

When I action post I don´t received nothing into model.Pdf, so into if (model.Pdf != null) validation it comes null, and I don´t know why

Anyone have idea why it occurs? Thankyou in advance!

Gerry
  • 245
  • 2
  • 4
  • 17
  • Unclear what your asking. Your form does not have a form control for property `Pdf` so it will always be `null` when you submit (but its unclear why you would need to post that value anyway) –  Jul 12 '16 at 03:01
  • I want to upload pdf file from user, and save it into path, then in my index view I want to get it to show it @StephenMuecke – Gerry Jul 12 '16 at 03:04
  • What does public string Pdf { get; set; } stored? file name? – MichaelMao Jul 12 '16 at 03:06
  • Thats fine (I assume you redirecting to the `Index()` method after saving?), but the issue is that you never get to save the file because the code in `if (model.Pdf != null)` is never executed because `model.Pdf` is always `null` –  Jul 12 '16 at 03:07
  • 2
    Best guess is that you want `if (model.PdfFile != null && model.PdfFile .ContentLength > 0) {` –  Jul 12 '16 at 03:09
  • I think @StephenMuecke is right. – MichaelMao Jul 12 '16 at 03:10
  • 2
    In addition, your input need to be `` in order to bind to your model. –  Jul 12 '16 at 03:12
  • I think `` requires `HttpPostedFileBase` which capture uploaded file and insert all contents into the model as byte stream with given file name, to check if file exists use `model.PdfFile != null && model.PdfFile.ContentLength > 0`. – Tetsuya Yamamoto Jul 12 '16 at 03:24
  • Yes it works, Thanks! @StephenMuecke – Gerry Jul 12 '16 at 03:50

2 Answers2

1

You have 2 main issues. First the name of your file input is name="file", but that does not match the property in your model. It needs to be

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

Second, you never generate an input for property string Pdf so in the POST method, it will always be null and therefore the code inside your if (model.Pdf != null) will never be executed. However, what you really want is to save the file if its not null, so the code needs to be

public async Task<string> CreateSubcategory(SubcategoryViewModel model)
{
   string fileName = null;
   if (model.PdfFile != null && model.PdfFile .ContentLength > 0)
   {
       fileName = Guid.NewGuid().ToString().Substring(0, 13) + "_" + model.PdfFile.FileName;
       string path = Path.Combine(HttpContext.Current.Server.MapPath("~/Content/pdf"), fileName);
       model.PdfFile.SaveAs(path);
    }
    var subcategory = new Subcategory
    {
        Pdf = fileName ,
    };
    db.SubcategoriesList.Add(subcategory);
    await db.SaveChangesAsync();

Side note: I would also recommend an additional property in your model for the files display name (i.e. the value of model.PdfFile.FileName) so that you can use that in your view rather than displaying the name prefixed with a Guid which would have little meaning to the user. Refer this answer for an example.

Community
  • 1
  • 1
0

I think your problem is you are getting null value in PdfFile property of your model. This is because you have not given name attribute of file upload control same as you property. change name attribute of your file upload control to PdfFile.

<input type="file" id="file" name="PdfFile" />
Manish Parakhiya
  • 3,732
  • 3
  • 22
  • 25