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())));
        }
    }
}

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...