Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to HttpPostedFileBase

I am trying to do upload attachment functionality using MVC. My method that is actually doing the upload/save attachment is expecting a HttpPostedFileBase type.

public virtual string Upload(HttpPostedFileBase fileName) 
{
     //Code to upload/save attachment.
}

My problem is "fileName" is being passed from the UI as a string. How can I convert the string(file path name) into something my Upload method can work with.

Thanks in advance.

like image 606
ZVenue Avatar asked Mar 16 '26 02:03

ZVenue


1 Answers

As mentioned by others, your form should look something like this:

<form id="form_UploadFile" action="" method="post" enctype="multipart/form-data">
  <label for="file">Filename:</label>
  <input type="file" name="file" id="file" />
</form>

Then as you mentioned you are trying to post via ajax, you can use jQuery serialize() to serialize the formData to be pushed to your controller.

$('#form_UploadFile').serialize();
like image 165
Josh Mein Avatar answered Mar 17 '26 14:03

Josh Mein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!