0 Replies - 2691 Views - Last Post: 01 September 2011 - 11:17 AM

#1 mb2000inc   User is offline

  • D.I.C Regular
  • member icon

Reputation: 24
  • View blog
  • Posts: 354
  • Joined: 10-November 09

Image Upload Command

Posted 01 September 2011 - 11:17 AM

Description: On the front end add a file upload (I named mine, "ImageUploader") control and a button (I named mine, "BtnUpload"). Copy and Paste this into your page's .CS file and add it to your project. Make sure you include this reference: "using System.IO;". This snippet takes into consideration, three types of JPEG formats... there are other formats out there, but these are the most common. (I did not allow for .GIF or .PNG or other formats - JPEG only) You will also need to set permissions for the folder that you are uploading to. You need to give Read/Write access to Network Services.If you want to be able to upload an image to your site - this works.
    protected void BtnUpload_click(object sender, EventArgs e)
    {   
            if (ImageUploader.HasFile)
            {
                try
                {
                    if (ThumbnailUploader.PostedFile.ContentType == "image/jpg" || (ThumbnailUploader.PostedFile.ContentType == "image/jpeg") || (ThumbnailUploader.PostedFile.ContentType == "image/pjpeg"))
                    {
                        string filename = Path.GetFileName(ThumbnailUploader.FileName);
                        ThumbnailUploader.SaveAs(Server.MapPath("~/imageFolder/") + filename);
                        StatusLabel.Text = "Upload status: File uploaded!";
                    }
                    else
                    {
                        StatusLabel.Text = "Upload status: The file must be JPEG format!";
                    }

                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
     }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1