【问题标题】:Upload Image from URI to Azure BLOB将图像从 URI 上传到 Azure BLOB
【发布时间】:2014-10-10 20:26:47
【问题描述】:

我想将图像从 uri postet 上传到 asp.net mvc5 控制器到 azure blob 存储。我已经让它与 HttpPostedFileBase 一起工作,就像这样。我能以某种方式从图像 uri 中获取内存流吗?

HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
var imgFile = System.Drawing.Image.FromStream(hpf.InputStream, true, true);
CloudBlockBlob blob = coversContainer.GetBlockBlobReference("img.jpg");
MemoryStream stream = new MemoryStream();
imgFile.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;
blob.UploadFromStream(stream);

【问题讨论】:

  • 您可以使用网络客户端下载数据并将其作为 MemoryStream(data) 返回
  • 感谢您为我指明正确的方向

标签: asp.net asp.net-mvc asp.net-mvc-5 azure-blob-storage


【解决方案1】:

这就是我设法完成它的方法:

public static Image DownloadRemoteImage(string url)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse response;
    try
    {
        response = (HttpWebResponse)request.GetResponse();
    }
    catch (Exception)
    {
        return null;
    }

    // Check that the remote file was found. The ContentType
    // check is performed since a request for a non-existent
    // image file might be redirected to a 404-page, which would
    // yield the StatusCode "OK", even though the image was not
    // found.
    if ((response.StatusCode == HttpStatusCode.OK ||
        response.StatusCode == HttpStatusCode.Moved ||
        response.StatusCode == HttpStatusCode.Redirect) &&
        response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
    {
        // if the remote file was found, download it
        Stream inputStream = response.GetResponseStream();
        Image img = Image.FromStream(inputStream);
        return img;
    }
    else
    {
        return null;
    }
}

这段代码是从这个问题的答案中截取和修改的: Download image from the site in .NET/C#

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 2020-06-03
    • 2021-01-16
    • 1970-01-01
    • 2018-12-17
    • 2015-05-15
    • 1970-01-01
    • 2017-01-06
    • 2013-07-29
    相关资源
    最近更新 更多