upload an image and get its bytesAsk Questions

 Posted on 22 days ago

I have such a form

<form name="" method="post" action="Save" enctype="multipart/form-data">

<div id="dialog" title="Upload files">

<input type="file" id="Image" name="fileUpload" size="23"/>

</div>

<input type="submit" value="Create" />

</form>

 

how sould i get the get the images bytes in the Controller?

Share On: facebook gplus twitter
profile
Asked by Helder Sepulveda on 22 days ago Score: 42 points
Add Comment:

Comments

2 Answers

0 Corrected Answers
Aproved Answers
0
Profile
Answered by hardik chaudhary on 10/2/2019 11:25:37 AM Score: 542 points

foreach (string file in Request.Files)

{

HttpPostedFileBase hpf = Request.Files[file];

// hpf.ContentLength has the file size in bytes

...

}

Comments

Add Comment:
Aproved Answers
0
Profile
Answered by Grayson Omans on 9/29/2019 8:43:29 PM Score: 59 points

Add the following to your controller method.

var file = Request.Files["fileUpload"]; 

if ( file != null )

{

byte[] fileBytes = new byte[file.ContentLength];

file.InputStream.Read( fileBytes, 0, file.ContentLength ); // ... now fileBytes[] is filled with the contents of the file.

}

else

{ // ... error handling here }

Comments

Add Comment:

Post Your Answers

Existing Members

Sign in to your account
Email Address
Password
New Member?
Sign up and complete profile
Full Name
Email Address
I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the StoodQ newsletters
Guideline to answer a question:

Useful tips to submit your answer
Please read below guidelines before you submit your answer for question.

  • Read and understand question for which you are submitting your answer.
  • Try to avoid grammatical and spell mistake while answering.
  • Do not post any irrelevant information in your answer.
  • Explain your answer with example or any reference link to help who posted question.
  • If you find irrelevant question, please report it to support. Click here to contact support.
  • You agree to the privacy policy and terms of use to submit any contents.

Note: StoodQ is online developers community which helps developer for their difficulty, lets help them with your value contribution.