2012-06-06 6 views
3

mvc 프로젝트에서 작업을 만들려고합니다. 파일과 이미지를 푸른 색 저장소에 업로드 할 수 있습니다. 그러나 웬일인지 그것이 단지 내가 단지 추측하고있는 것 인 단지 공손히 업로딩하지 않을 것이다. "Azure Storage Explorere"로 이미지를 업로드하면 정상적으로 작동합니다. 예 : http://storage.sogaard.us/company1/wallpaper-396234.jpg 하지만 내 동작이 작동하지 않더라도 이미지를 업로드하려고하면 이미지가 제대로로드되지 않고 이미지가로드되지 않으며 개발자 도구에서 데이터가 없다고 알립니다. 서버에서. 예 : http://storage.sogaard.us/company1/b9206edac188e1d8aa2b3be7cdc4b94a.jpg하늘색 저장소가 이미지를 올바르게 업로드 할 수 없습니다.

푸른 색 저장소 대신 로컬 컴퓨터에 업로드 한 메시지를 저장하려고했는데 제대로 작동했습니다. 나는 단순히 그 이유를 찾을 수없는이 나에게 하루 종일 여기 :(

를 도청 한 것은 썸네일 코드 경로에서 내 코드

[HttpPost] 
public ActionResult Upload(FilesUploadModel model, IEnumerable<HttpPostedFileBase> files) 
{ 

    if(ModelState.IsValid) 
    { 
     if (files != null && files.Any()) 
     { 
      int maxSizeInBytes = ConfigurationManager.Instance.Configuration.FileMaxSize; 
      Size thumbSize = new Size(200, 200); 

      foreach (HttpPostedFileBase file in files.Where(x => x != null)) 
      { 
       CMS.Common.Data.File _file = new Sogaard.Inc.CMS.Common.Data.File(); 

       // is any files uploadet? 
       if (!(file.ContentLength > 0)) 
       { 
        FlashHelper.Add(Text("File not received"), FlashType.Error); 
        continue; 
       } 
       // is the file larger then allowed 
       if (file.ContentLength > maxSizeInBytes) 
       { 
        FlashHelper.Add(Text("The file {0}'s file size was larger then allowed", file.FileName), FlashType.Error); 
        continue; 
       } 

       var fileName = Encrypting.MD5(Path.GetFileNameWithoutExtension(file.FileName) + DateTime.Now) + Path.GetExtension(file.FileName); 
       string mimeType = FileHelper.MimeType(FileHelper.GetMimeFromFile(file.InputStream)); 

       _file.SiteId = SiteId(); 
       _file.Container = GetContainerName(); 
       _file.FileName = Path.GetFileName(file.FileName); 
       _file.FileNameServer = fileName; 
       _file.Created = DateTime.Now; 
       _file.Folder = model.Folder; 
       _file.Size = file.ContentLength; 
       _file.Type = mimeType; 

       if (mimeType.ToLower().StartsWith("image/")) 
       { 
        try 
        { 
         // So we don't lock the file 
         using (Bitmap bitmap = new Bitmap(file.InputStream)) 
         { 
          _file.Information = bitmap.Width + "|" + bitmap.Height; 

          if (bitmap.Height > 500 && bitmap.Width > 500) 
          { 
           var thumbfileName = Encrypting.MD5(Path.GetFileNameWithoutExtension(file.FileName) + "thumb" + DateTime.Now) + ".jpeg"; 
           Size thumbSizeNew = BaseHelper.ResizeImage(bitmap.Size, thumbSize); 
           Bitmap thumbnail = (Bitmap)bitmap.GetThumbnailImage(thumbSizeNew.Width, 
                          thumbSizeNew.Height, 
                          ThumbnailCallback, 
                          IntPtr.Zero); 
           _file.ThumbFileNameServer = thumbfileName; 
           // Retrieve reference to a blob named "myblob" 
           CloudBlob blob = container().GetBlobReference(_file.ThumbFileNameServer); 
           blob.Metadata["Filename"] = Path.GetFileNameWithoutExtension(file.FileName) + "-thumb" + ".jpg"; 
           blob.Properties.ContentType = "image/jpeg"; 

           // Create or overwrite the "myblob" blob with contents from a local file 
           using (MemoryStream memStream = new MemoryStream()) 
           { 
            thumbnail.Save(memStream, ImageFormat.Jpeg); 
            blob.UploadFromStream(memStream); 
           } 
           blob.SetMetadata(); 
           blob.SetProperties(); 
          } 
         } 
        } 
        catch (Exception e) 
        { 
         if (e.GetType() != typeof (DataException)) 
          FlashHelper.Add(Text("The image {0} was not a valid image", file.FileName), 
              FlashType.Error); 
         // Removing the file 
         System.IO.File.Delete(file.FileName); 
        } 
       } 
       else 
       { 
        _file.Information = null; 
       } 
       // Retrieve reference to a blob named "myblob" 
       CloudBlob blobF = container().GetBlobReference(fileName); 
       blobF.Metadata["Filename"] = file.FileName; 
       blobF.Properties.ContentType = mimeType; 

       // Create or overwrite the "myblob" blob with contents from a local file 
       blobF.UploadFromStream(file.InputStream); 
       blobF.SetMetadata(); 
       blobF.SetProperties(); 

       fileService.Save(_file); 
      } 
     } 

     return RedirectToAction("Display", new { Folder = model.Folder }); 
    } 

    model.FolderSugestion = fileService.GetFolders(SiteId()); 
    return View(model); 
} 

    private CloudBlobContainer container() 
    { 
     // Retrieve storage account from connection-string 
     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
      RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString")); 

     // Create the blob client 
     CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

     // Retrieve reference to a previously created container 
     var container = blobClient.GetContainerReference(GetContainerName()); 
     container.CreateIfNotExist(); 
     return container; 
    } 

    private string GetContainerName() 
    { 
     return "company" + SiteId(); 
    } 

답변

1

, 난 당신이 다시 스트림을 재설정 memStream.Position = 0 필요하다고 생각 업로드하기 전에 시작. 다른 (비 이미지) 코드 경로에 대한

, 아무것도 그 코드가 작동. 같은 잘못에 띄는 없습니다합니까? 모두 코드 경로에서

, 당신은 blob.SetMetadata()blob.SetProperties() 필요하지 않습니다 ,시 업로드가 완료되면 업로드가 완료됩니다.

[편집] 또한 GetMimeFromFile은 무엇을 수행합니까? 스트림에서 읽습니까 (따라서 스트림 위치를 처음이 아닌 다른 위치로 남겨 둘 수 있습니다)?

+0

비트 맵을 초기화하면 스트림 위치도 변경됩니다. 새 비트 맵 (file.InputStream). 따라서 미리보기 이미지를 업로드 할 때 memStream의 위치를 ​​재설정하는 것 외에 blobF.UploadFromStream (file.InputStream) (실제 파일을 업로드 할 때) –

+0

오! 나는 조건부의 범위를 잘못 읽었습니다 ... 당신은 옳습니다. – smarx

+0

지금까지는 이미지 만 테스트했습니다. GetMimeFromFile은 스트림 yes를 읽습니다. – Androme

관련 문제