fileupload newline in constant
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开发者_如何学C 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)
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.
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/
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
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.
精彩评论