2010-05-25 8 views
18

이미지 파일을 BLOB에 업로드하면 이미지가 분명히 성공적으로 업로드됩니다 (오류 없음). 클라우드 스토리지 스튜디오에 갈 때 파일은 있지만 크기는 0 (제로) 바이트입니다.하늘빛 저장소 : 크기가 0 바이트 인 업로드 된 파일

가 다음 내가 사용하고있는 코드입니다 : 이미지 파일이 저장에 업로드하기 전에

// These two methods belong to the ContentService class used to upload 
// files in the storage. 
public void SetContent(HttpPostedFileBase file, string filename, bool overwrite) 
{ 
    CloudBlobContainer blobContainer = GetContainer(); 
    var blob = blobContainer.GetBlobReference(filename); 

    if (file != null) 
    { 
     blob.Properties.ContentType = file.ContentType; 
     blob.UploadFromStream(file.InputStream); 
    } 
    else 
    { 
     blob.Properties.ContentType = "application/octet-stream"; 
     blob.UploadByteArray(new byte[1]); 
    } 
} 

public string UploadFile(HttpPostedFileBase file, string uploadPath) 
{ 
    if (file.ContentLength == 0) 
    { 
     return null; 
    } 

    string filename; 
    int indexBar = file.FileName.LastIndexOf('\\'); 
    if (indexBar > -1) 
    { 
     filename = DateTime.UtcNow.Ticks + file.FileName.Substring(indexBar + 1); 
    } 
    else 
    { 
     filename = DateTime.UtcNow.Ticks + file.FileName; 
    } 
    ContentService.Instance.SetContent(file, Helper.CombinePath(uploadPath, filename), true); 
    return filename; 
} 

// The above code is called by this code. 
HttpPostedFileBase newFile = Request.Files["newFile"] as HttpPostedFileBase; 
ContentService service = new ContentService(); 
blog.Image = service.UploadFile(newFile, string.Format("{0}{1}", Constants.Paths.BlogImages, blog.RowKey)); 

의 HttpPostedFileBase에서 속성의 InputStream 괜찮을 것 같습니다 (이미지의 크기에 해당 예상되는 것! 예외는 발생하지 않습니다.)

정말 이상한 점은 다른 경우 (파워 포인트 또는 심지어 작업자 역할의 다른 이미지 업로드)에서 완벽하게 작동한다는 것입니다. SetContent 메서드를 호출하는 코드는 정확히 같고 0 바이트의 새 파일이 올바른 위치에 만들어 지므로 파일이 올바른 것 같습니다.

아무도 제안을 해주지 않습니까? 이 코드를 수십 번 디버깅했는데 문제가 보이지 않습니다. 어떤 제안이라도 환영합니다!

감사

답변

43

HttpPostedFileBase의의 InputStream의 위치 속성은 길이 속성과 동일한 값을 가지고 있었다 (나는이 일에 이전의 다른 파일이 있었다 아마도 때문에 -! 바보 같은 내 생각을).

Position 속성을 다시 0으로 설정하면됩니다.

앞으로 도움이되기를 바랍니다.

+6

이 약간 명확히하기 위해, 당신은로드 후 0으로 스트림의 위치 속성을 설정해야합니다 확인 그것으로 어떤 바이트 든. 기본적으로 스트림의 위치는 어떤 이유로 든 콘텐츠의 끝으로 설정됩니다. – Dusda

+0

네, 그건 제가 항상 알고있는 것입니다. 그리고 나는 그것을 기억하기 위해 1 초 이상을 결코 풀어 내지 못합니다. 감사! –

+0

고마워요 :) –

15

Fabio에게이 문제를 제기하고 직접 해결해 주셔서 감사합니다. 난 당신이 말한 것에 코드를 추가하고 싶습니다. 귀하의 제안은 저에게 완벽하게 작용했습니다.

 var memoryStream = new MemoryStream(); 

     // "upload" is the object returned by fine uploader 
     upload.InputStream.CopyTo(memoryStream); 
     memoryStream.ToArray(); 

// After copying the contents to stream, initialize it's position 
// back to zeroth location 

     memoryStream.Seek(0, SeekOrigin.Begin); 

그리고 지금은 사용 MemoryStream을 업로드 할 준비가 : 당신이 스트림 작업 할 때

blockBlob.UploadFromStream(memoryStream);