2016-11-07 119 views
1

可以讓CloudBlockBlob.UploadFromStreamAsync接受圖像的URL嗎? 我試圖使用LinkedIn basicprofile api來檢索用戶的圖片URL(已經有URL),並且我試圖下載然後將圖片上傳到Azure Blob,就像他們從他們的計算機中選擇了一張圖片一樣。從URL上傳圖像到Azure Blob存儲

這是怎麼看起來像現在:

using (Html.BeginForm("UploadPhoto", "Manage", FormMethod.Post, new { enctype = "multipart/form-data" })) 
      { 
       <div class="browseimg"> 
        <input type="file" class="display-none" name="file" id="files" onchange="this.form.submit()" /> 
       </div> 
      } 
      <button class="btn btn-primary width-100p main-bg round-border-bot" id="falseFiles"> 
       Upload billede 
      </button> 

在控制器的方法:

public async Task<ActionResult> UploadPhoto(HttpPostedFileBase file) 
     { 

      if (file != null && file.ContentLength > 0) 
      { 
       var fileExt = Path.GetExtension(file.FileName); 
       if (fileExt.ToLower().EndsWith(".png") || fileExt.ToLower().EndsWith(".jpg") || 
        fileExt.ToLower().EndsWith(".gif")) 
       { 
        var user = await GetCurrentUserAsync(); 
        await service.Upload(user.Id, file.InputStream); 
       } 
      } 
      return RedirectToAction("Index"); 
     } 
+0

沒有,是不可能的,但是從我看到你在表單上載文件不是一個URL,你手動下載的圖片? – SilentTremor

+0

是的,這是從您的PC中選擇文件並上傳它的方法。不知怎的,它應該是可能的。我將嘗試從閱讀網址和上傳該流或其他東西中將它放入流中 – crystyxn

+0

我正在爲您創建一些簡單的事情, – SilentTremor

回答

0

貌似我只需要一個簡單的

WebClient wc = new WebClient(); 
MemoryStream stream = new MemoryStream(wc.DownloadData("https://media.licdn.com/mpr/...")); 

然後

await service.Upload(user.Id, stream); 
2

下面方法上傳文件(URL)天青cloudblob

注:輸入這種方法例如

文件= 「http://example.com/abc.jpg」 和ImageName =「MYIMAGE .JPG「;

public static void UploadImage_URL(string file, string ImageName) 
     { 
      string accountname = "<YOUR_ACCOUNT_NAME>"; 

      string accesskey = "<YOUR_ACCESS_KEY>"; 

      try 
      { 

       StorageCredentials creden = new StorageCredentials(accountname, accesskey); 

       CloudStorageAccount acc = new CloudStorageAccount(creden, useHttps: true); 

       CloudBlobClient client = acc.CreateCloudBlobClient(); 

       CloudBlobContainer cont = client.GetContainerReference("<YOUR_CONTAINER_NAME>"); 

       cont.CreateIfNotExists(); 

       cont.SetPermissions(new BlobContainerPermissions 
       { 
        PublicAccess = BlobContainerPublicAccessType.Blob 

       }); 
       HttpWebRequest request = (HttpWebRequest)WebRequest.Create(file); 
       HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
       Stream inputStream = response.GetResponseStream(); 
       CloudBlockBlob cblob = cont.GetBlockBlobReference(ImageName); 
       cblob.UploadFromStream(inputStream); 
      } 
      catch (Exception ex) 
      { 

      } 

     } 
+0

謝謝你的回答Supraj :) – crystyxn

相關問題