简体   繁体   中英

Trying to upload a PDF file using ASP.NET MVC is causing an error "Object reference not set to an instance of an object"

I am trying to upload a PDF file to a folder and save the file path to the database in ASP.NET MVC. I can use this same method to save a JPG file while it is throwing an error for a PDF file.

Am I missing out anything?

This is my code:

DirectoryInfo dir = new DirectoryInfo(HttpContext.Server.MapPath("~/LoanDocuments/Uploads/"));

if (!dir.Exists)
{
    dir.Create();
}

// Uploading PDF and saving to database
// Extract PDF File Name.
string fileName = initiateLoanRequest.Firstname + initiateLoanRequest.Surname + Loan_RequestID; //The filename will be FirstnameSurnameLoan_RequestID

// Set the PDF File Path.
string filePath = "/LoanDocuments/Uploads/" + fileName + ".pdf";

// Save the uploaded document in Folder.
loan_Document.SaveAs(Server.MapPath(filePath));

What am I missing out in this? It works well when I used same code in another module for JPG files.

Please note that the loan_Document is posted as HttpPostedFileBase loan_Document .

Issue resolved. The Posted file name I am sending from the View is different from the postedfile name I am using at the Controller. So using the same names resolved the issue.

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM