views:

116

answers:

3

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?

+1  A: 
HttpFileCollection files;
InputStream input;
int loop1;
string arr1;

files = Request.Files;
arr1 = Files.AllKeys;

for (loop1 = 0; loop1 < arr1.Length; loop1++) {
  input = files[loop1].InputStream;
  // Use input to access the file content.
}

I am sorry; I misread what the question was.

kiamlaluno
it's ok :) <br> u forgot to define loop1s type ;)
CoffeeCode
OK; now I didn't forget any declarations. :-)
kiamlaluno
+2  A: 
foreach (string file in Request.Files)
    {
        HttpPostedFileBase hpf = Request.Files[file];
        // hpf.ContentLength has the file size in bytes
        ...
    }
msha
+2  A: 

Add the following to your controller method.

      var file = Request.Files["Image"];
      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
      }
womp
its supposed to bevar file = Request.Files["uploadFile"];it takes the elements name as an Indexer:)
CoffeeCode