C# 如何将图像上载到文件夹并显示
我试着通过按一个提交按钮将图片上传到整个表单的文件夹(使用FileUpload)。我设法将图像上载到单独的文件夹,但无法显示它。 多谢各位C# 如何将图像上载到文件夹并显示,c#,asp.net,C#,Asp.net,我试着通过按一个提交按钮将图片上传到整个表单的文件夹(使用FileUpload)。我设法将图像上载到单独的文件夹,但无法显示它。 多谢各位 String fname; FileUpload tempFU = new FileUpload(); string path = Server.MapPath(".") + "\\images\\" + ulProj.groupCode; if (!Directory.Exists(path)) {
String fname;
FileUpload tempFU = new FileUpload();
string path = Server.MapPath(".") + "\\images\\" + ulProj.groupCode;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
try
{
tempFU = (FileUpload)customerUC.FindControl("CustomerLogoUrlFU");
Directory.CreateDirectory(path);
fname = path + "\\" + tempFU.FileName;
tempFU.SaveAs(fname);
tempCus.logoUrl = fname;
}
catch
{
//return;
}
要记住的要点:
tilde
~运算符来表示当前项目
根文件夹
System.IO.Path.Combine()
组合您的路径
和文件名
以获得有效的完整路径路径创建目录
两次。因此,删除第二次创建目录的后面部分
注释中所述,由于catch
块没有任何代码,
删除try catch
块
String fname;
FileUpload tempFU = new FileUpload();
string path = Server.MapPath(@"~\images\" + ulProj.groupCode);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
tempFU = (FileUpload)customerUC.FindControl("CustomerLogoUrlFU");
fname = System.IO.Path.Combine(path,tempFU.FileName);
tempFU.SaveAs(fname);
tempCus.logoUrl = fname;
您应该只将图像的(有效)路径指定给图像标记的src属性。你的问题是什么?我强烈建议你去掉那个try/catch块。我做了,但它仍然没有显示出来it@user3025441:tempCus是图像控件吗?