I'm creating a form with "Upload" control by HTML input type="file"
Here is my html code:
<form id="form1" runat="server" enctype="multipart/form-data">
<div class="form-group">
<asp:label ID="Label1" runat="server" For="message-text" class="col-form-label">Transaction Slip:</asp:label><br />
<input type="file" name="FileUpload" class="btn btn-light" accept="image/*"/>
</div>
</form>
And the .cs behind code like below:
protected void btnSubmit_Clicked(object sender, EventArgs e)
{
HttpPostedFile postedFile = Request.Files["FileUpload"];
string filePath = Server.MapPath("~/TransacSlip/") + Path.GetFileName(postedFile.FileName);
postedFile.SaveAs(filePath);
}
But I was getting the error below:
Server Error in '/' Application. Object reference not set to an instance of an object.
Error Line:
Line 78: string filePath = Server.MapPath("~/TransacSlip/") + Path.GetFileName(postedFile.FileName);
Did anyone know how to solve this problem ? Many Thanks ~