Saturday 15 September 2012

Upload file


 // Get a reference to PostedFile object
          HttpPostedFile myFile = FileUploadProof.PostedFile;

        // Get size of uploaded file
        int nFileLen = myFile.ContentLength;

        // Allocate a buffer for reading of the file
        byte[] myData = new byte[nFileLen];

        // Read uploaded file from the Stream
        myFile.InputStream.Read(myData, 0, nFileLen);

//check if directory folder is exists or not

if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/UploadFile/" + Session["Name"].ToString())))
  {                Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/UploadFile/" + Session["Name"].ToString()));

//it is used to save the uploaded file
                FileUploadProof.SaveAs(HttpContext.Current.Server.MapPath("~/UploadFile/" + Session["Name"].ToString() + "/" +  System.IO.Path.GetFileName( FileUploadProof.FileName)));

            }
            else
            {
         
                FileUploadProof.SaveAs(HttpContext.Current.Server.MapPath("~/UploadFile/" + Session["Name"].ToString() + "/" + System.IO.Path.GetFileName(FileUploadProof.FileName)));
            }


No comments:

Post a Comment