[英]Upload image to Azure Blob Storage from URL
Is it possible to have the CloudBlockBlob.UploadFromStreamAsync
accept URL for images? 是否可以让
CloudBlockBlob.UploadFromStreamAsync
接受图像的URL? I am trying to use the LinkedIn basicprofile api to retrieve the picture URL of the user (already have the URL) and I am trying to maybe download then upload the picture to the Azure Blob, like they selected a picture from their computer. 我正在尝试使用LinkedIn基本配置文件api来检索用户的图片URL(已经有该URL),并且试图下载然后将图片上传到Azure Blob,就像他们从计算机中选择了图片一样。
This is how it looks like now: 现在是这样的:
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>
The method in the controller: 控制器中的方法:
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")
}
Below method uploads file(URL) to azure cloudblob 下面的方法将文件(URL)上传到azure cloudblob
NOTE: Input for this method example 注意:此方法示例的输入
file="http://example.com/abc.jpg" and ImageName="myimage.jpg";
file =“ 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){ ... }
}
Looks like all I needed was a simple 看起来我只需要一个简单的
WebClient wc = new WebClient();
MemoryStream stream = new MemoryStream(wc.DownloadData("https://media.licdn.com/mpr/..."));
and then 接着
await service.Upload(user.Id, stream);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.