从URL

时间:2016-11-07 12:22:37

标签: c# asp.net-mvc azure azure-storage-blobs imageurl

是否可以使用CloudBlockBlob.UploadFromStreamAsync接受图片的网址?  我正在尝试使用LinkedIn basicprofile api来检索用户的图片URL(已经有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")
}

2 个答案:

答案 0 :(得分:4)

以下方法将文件(URL)上传到azure cloudblob

注意:此方法示例的输入

  

文件=&#34; HTTP://example.com/abc.jpg"和ImageName =&#34; myimage.jpg&#34 ;;

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){ ... }

}

答案 1 :(得分:0)

看起来我需要的只是一个简单的

{
    "status": "success",
    "products": {
        "Key1": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
        "Key2": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
        "Key3": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
    }
}

然后

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