我在常量中遇到问题换行符:
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;
}
}
}
}
我也想知道有没有办法在上传时调整图片大小,如果是这样,怎么样?
因为这是一个保存,因为我的fileupload控件会覆盖任何已经存在用户ID的文件夹吗? (我想要什么,需要什么)
答案 0 :(得分:2)
好吧,我已经可以在你的错误上面的行中看到你在/ uploadedimage /之后缺少一个引用,并且最后一个paren应该在文件名之后没有/ uploadedimage /
答案 1 :(得分:2)
常量中的换行符
FileUploadControl.SaveAs(Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/) + filename);
//here
应该是:
FileUploadControl.SaveAs(Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/") + filename);
//here
图片调整大小/缩略图
如果是您正在播放的缩略图,则可以使用Image.GetThumbnailImage。基本实现是这样的:
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);
}
}
<强>改写强>
覆盖是默认行为,因此将同一文件上传到同一个UserID将替换它。
答案 2 :(得分:1)
您是否错过了引用或是错字:
Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/")
我还建议在使用大量字符串连接时使用string.Format
(通常在必须多次使用+
时执行此操作):
Server.MapPath(string.Format("~/userdata/{0}/uploadedimage/", theUserId))
编辑以回复调整评论:
以下是类似SO问题的链接:
答案 3 :(得分:0)
newline变量有哪个值?
下面为收集的值添加了一个Trim(),并提供了一种查看之后收集的每个字符串值的方法。
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()字符串值中包含换行符。