Wednesday, December 2, 2009

File Upload in asp.net

The following post deals with file upload in asp.net,
In your aspx page:
<div>
        <asp:FileUpload ID="ImgUpload" runat="server" />
        <asp:Button ID="Upload" runat="server" Text="Upload" onclick="Upload_Click" />
        <asp:Image ID="samp" runat="server" ImageUrl="~/images/new//Testingimages1253899911515.gif " />
    </div>

In your aspx.cs page:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Text.RegularExpressions;

public partial class FileUpload1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private bool IsImage(HttpPostedFile file)
    {
        if (file != null && Regex.IsMatch(file.ContentType, "image/\\S+") &&
          file.ContentLength > 0)
        {
            return true;
        }
        return false;
    }
    public string SaveImageFile(FileUpload fu, string directoryPath, int MaxWidth, int MaxHeight, string prefixName)
    {
        string serverPath = "", returnString = "";
        if (fu.HasFile)
        {
            Byte[] bytes = fu.FileBytes;
            //Int64 len = 0;
            prefixName = "Testing" + prefixName;
            //directoryPath = "Testing/";
            System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            string dipath = System.Web.HttpContext.Current.Server.MapPath("~/") + directoryPath;
            DirectoryInfo di = new DirectoryInfo(dipath);
            if (!(di.Exists))
            {
                di.Create();
            }
            HttpPostedFile file = fu.PostedFile;
            DateTime oldTime = new DateTime(1970, 01, 01, 00, 00, 00);
            DateTime currentTime = DateTime.Now;
            TimeSpan structTimespan = currentTime - oldTime;
            prefixName += ((long)structTimespan.TotalMilliseconds).ToString();
            if (IsImage(file))
            {
                using (Bitmap bitmap = new Bitmap(file.InputStream, false))
                {
                    serverPath = dipath + "//" + prefixName + fu.FileName.Substring(fu.FileName.IndexOf("."));
                    img.Save(serverPath);
                    returnString = "~/" + directoryPath + "//" + prefixName + fu.FileName.Substring(fu.FileName.IndexOf("."));
                }
            }
        }
        return returnString;
    }

    protected void Upload_Click(object sender, EventArgs e)
    {
        string imageUrl;
        imageUrl = SaveImageFile(ImgUpload, "images/new", 600, 600, "images");
        Response.Write(imageUrl);
    }
}

No comments:

Post a Comment