2009-12-19 5 views
1

나는 이런 형식을 가지고있다 이미지를 업로드하고 그 바이트를 얻는다.

<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> 

어떻게하면 컨트롤러에서 이미지 바이트를 얻을 수 있을까요?

답변

11

다음을 컨트롤러 방법에 추가하십시오.

 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 
     } 
+2

그 가정해야합니다 var file = Request.Files [ "uploadFile"]; 요소 이름을 인덱서로 사용합니다 :) – CoffeeCode

1
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. 
} 

죄송합니다. 나는 그 질문이 무엇인지 잘못 읽었다.

+3

괜찮아요 :)
유 loop1s 유형을 정의하는 것을 잊었다) – CoffeeCode

+0

OK; 지금 나는 어떤 선언도 잊지 않았다. :-) – kiamlaluno

2
foreach (string file in Request.Files) 
    { 
     HttpPostedFileBase hpf = Request.Files[file]; 
     // hpf.ContentLength has the file size in bytes 
     ... 
    } 
관련 문제