2016-11-07 144 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