Uploading and Downloading image or files

This is just to give you an idea. The original codes was done in DNN 3.2.2.
Uploading Images and Files.
  protected void btnUpload_Click(object sender, EventArgs e)
  {
   try
   {
    // If file field isn’t empty
    if (FieldFile.Value  != null)
    {
     // Check file size (mustn’t be 0)
     HttpPostedFile myFile = FieldFile.PostedFile;
     int nFileLen = myFile.ContentLength;
     if (nFileLen == 0)
     {
      lblOutput.Text = “No file was uploaded.”;
      return;
     }
     // Check file extension (must be JPG)
     if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != “.jpg”)
     {
      lblOutput.Text = “The file must have an extension of JPG”;
      return;
     }
     // Read file into a data stream
     byte[] myData = new Byte[nFileLen];
     myFile.InputStream.Read(myData, 0, nFileLen);
     // Make sure a duplicate file doesn’t exist.  If it does, display File Exist
     // and delete the uploaded file.
     string sFilename = System.IO.Path.GetFileName(myFile.FileName);
     while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
     {
      if (Convert.ToInt32(Session[“Result”])> 0 )
      {
       lblOutput.Text = “File uploaded successfully!”;
      }
      else
      {
       lblOutput.Text = “File exist!”;
      }
      return;
     }
     // Save the stream to disk
     System.IO.FileStream newFile = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename),
      System.IO.FileMode.Create);
     newFile.Write(myData, 0, myData.Length);
     newFile.Close();
     // Check whether the file is really a JPEG by opening it
     System.Drawing.Image.GetThumbnailImageAbort myCallBack =
      new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
     Bitmap myBitmap;
     try
     {
      myBitmap = new Bitmap(Server.MapPath(sSavePath + sFilename));
      // If jpg file is a jpeg, create a thumbnail filename.            
      string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
       + “.jpg”;
      ViewState[“FileName”] = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName);
      ViewState[“ThumbFile”] = sThumbFile;
      //Save record to database
      Conn(“Add”);
      // Save thumbnail and output it onto the webpage
      System.Drawing.Image myThumbnail
       = myBitmap.GetThumbnailImage(intThumbWidth,
       intThumbHeight, myCallBack, IntPtr.Zero);
      // Displaying success information
      lblOutput.Text = “File uploaded successfully!”;
      // Destroy objects      
      System.IO.File.Delete(Server.MapPath(sSavePath + sFilename));
     }
     catch (Exception err)
     {
      // The file wasn’t a valid jpg file
      if (Convert.ToInt32(Session[“Result”])> 0 )
      {
       lblOutput.Text = “File uploaded successfully!”;
      }
      else
      {
              lblOutput.Text = err.Message.ToString();
      }
     }
    }
   }
   catch (Exception ex)
   {
    if (Convert.ToInt32(Session[“Result”])> 0 )
    {
     lblOutput.Text = “File uploaded successfully!”;
    }
    else
    {
     lblOutput.Text = ex.Message.ToString();
    }    
   }
  }
 
Downloading the images and files.
        protected void Button1_ItemCommand(object source, DataGridCommandEventArgs) 
        {
            try
            {
                string id = e.Item.Cells(1).Text.Trim();
                System.IO.File.Copy(e.Item.Cells(5).Text.Trim(), “C:\FilePath\” + e.Item.Cells(6).Text.Trim(), True);
                 ….
            }
            Catch (Exception ex)
            {
                Response.Write(ex.ToString() + “<BR>”);
            }
        }
 
Advertisements
This entry was posted in ASP.NET and DNN 3.2.2. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s