0

I get the problem newline in constant:

    protected void UploadButton_Click(object sender, EventArgs e)
    {
        string theUserId = Session["UserID"].ToString();
        if (FileUploadControl.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(FileUploadControl.FileName);
                FileUploadControl.SaveAs(Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/) + filename);
//here
                StatusLabel.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    }

}

Im also wondering is there a way I can resize the image upon upload if so, how?

And as this is a save as in my fileupload control will this overwrite any folder that has the userid already there? (what I want and need)

G Gr
  • 6,030
  • 20
  • 91
  • 184

4 Answers4

2

Well I can already see in the line above your error you are missing a quote after /uploadedimage/ and the next to last paren should be after filename not /uploadedimage/

novacara
  • 2,207
  • 4
  • 24
  • 34
2

Newline in constant

FileUploadControl.SaveAs(Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/) + filename); //here

Should be:

FileUploadControl.SaveAs(Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/") + filename); //here

Image resize / thumbnails

If it's a thumbnail image you're after, then you can use Image.GetThumbnailImage. A basic implementation is like this:

using (System.Drawing.Image fullsizeImage =
         System.Drawing.Image.FromFile(originalFilePath))
{
       // these calls to RotateFlip aren't essential, but they prevent the image 
       // from using its built-in thumbnail, which is invariably poor quality.
       // I like to think of it as shaking the thumbnail out ;)
       fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
       fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

       using (System.Drawing.Image thumbnailImage = 
         fullsizeImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero))
       {
            thumbnailImage.Save(newFilePath);
       }
}

Overwrite

Overwrite is the default behaviour, so uploading the same file to the same UserID will replace it.

Town
  • 14,706
  • 3
  • 48
  • 72
  • no not thumbnails just a resize in jpeg pixel need the images to be set to a certain width and height (pixel) – G Gr Mar 24 '11 at 21:35
  • Then this will be what you're after: http://stackoverflow.com/questions/249587/high-quality-image-scaling-c – Town Mar 24 '11 at 21:44
1

Are you missing a quote or is it a typo:

Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/")

I would also recommend using string.Format when using lots of string concatenation (usually do this when I have to use + more than once):

Server.MapPath(string.Format("~/userdata/{0}/uploadedimage/", theUserId))

EDIT: to answer resize comment:

Here is a link to a similar SO question:

how to resize an image whileing saving in an folder

Community
  • 1
  • 1
Kelsey
  • 47,246
  • 16
  • 124
  • 162
0

Which value has the newline variable?

Below adds a Trim() to the value collected and also provides a way to see what each of the string values collected afterwards are.

protected void UploadButton_Click(object sender, EventArgs e)
{
  string theUserId = Session["UserID"].ToString().Trim(); // add Trim()
  if (FileUploadControl.HasFile && !String.IsNullOrEmpty(theUserId)) // add test
  {
    try
    {
      string filename = Path.GetFileName(FileUploadControl.FileName);
      Console.WriteLine(filename);
      string filepath = string.Format("~/userdata/{0}/uploadedimage/", theUserId);
      Console.WriteLine(filepath );
      string mapPath = Server.MapPath(filepath);
      Console.WriteLine(mapPath );
      FileUploadControl.SaveAs(mapPath + filename);
      StatusLabel.Text = "Upload status: File uploaded!";
    }
    catch (Exception ex)
    {
      StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
    }
  }
}

Just Trim() the string value has the newline constant in it.