Friday, August 19, 2011

How to upload a external image link url and save on the disk

 We can get the use of HttpWebRequest and HttpWebResponse to send the request and get the response. Then response.GetResponseStream() will give us a stream of the image where we can use that stream to create a Image object like Image image = Image.FromStream(stream)

Markup:
<%@ Page Language="C#" CodeBehind="Test.aspx.cs" Inherits="ActiveTest.Test" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox runat="server" ID="txtUpload" 
            Text="http://www.web-sphere.co.uk/web/websphere/blog/bloggerheader.jpg" />
        <asp:Button runat="server" ID="btnUpload" OnClick="Upload" Text="Upload" />
    </div> 
    </form>
</body>
</html>
Code:
namespace ActiveTest
{
    public partial class Test : Page
    {
        protected void Upload(object sender, EventArgs e)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.txtUpload.Text);
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            Stream stream = response.GetResponseStream();         
            ///
            /// use response content type to findout the image type, 
            /// here I just use jpg to simplify the story.
            /// 
            System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
            image.Save(this.Server.MapPath(string.Format("~/Uploads/{0}.jpg",Guid.NewGuid())));
        }
    }
}

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...