Thursday, 13 March 2014

Upload File without using server control in asp.net

.Aspx Page or .master Page

 <form action="Default.aspx" method="post" enctype="multipart/form-data">
    <input type="file" name="UploadedFile" />
    <input type="submit" value="OK" />
  </form>

Default.aspx.cs Page

protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Files["UploadedFile"] != null)
        {
            HttpPostedFile MyFile = Request.Files["UploadedFile"];
            //Setting location to upload files
            string TargetLocation = Server.MapPath("~/Files/");
            try
            {
                if (MyFile.ContentLength > 0)
                {
                    //Determining file name. You can format it as you wish.
                    string FileName = Path.GetFileName(MyFile.FileName);
                    //Determining file size.
                    //int FileSize = Dosya.ContentLength;
                    //Creating a byte array corresponding to file size.
                    //byte[] FileByteArray = new byte[FileSize];
                    //Posted file is being pushed into byte array.
                   // MyFile.InputStream.Read(FileByteArray, 0, FileSize);
                    //Uploading properly formatted file to server.
                    MyFile.SaveAs(TargetLocation + FileName);
                    }
            }
            catch (Exception BlueScreen)
            {
                //Handle errors
            }
        }


No comments:

Post a Comment