我有这个代码将FileUpload中的图像保存到文件夹:
int count = 1;
string fullPath = Path.GetFullPath(FileUpload1.FileName);
string fileNameOnly = Path.GetFileNameWithoutExtension(fullPath);
string extension = Path.GetExtension(fullPath);
string path = Path.GetDirectoryName(fullPath);
string tempFileName = fileNameOnly;
while (File.Exists("~/ UploadedImages//" + tempFileName + extension))
{
tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
}
FileUpload1.SaveAs(Server.MapPath("~/ UploadedImages//" + tempFileName + extension));
答案 0 :(得分:-2)
string ext = Path.GetExtension(FileUpload1.FileName);
if (ext.ToLower() == ".jpg" || ext.ToLower() == ".png" || ext.ToLower() == ".gif" || ext.ToLower() == ".ico")
{
string fn = Guid.NewGuid() + Path.GetExtension(file.FileName);
file.SaveAs(Path.Combine(Server.MapPath("~/assets/image/"),fn));
}
if (ext.ToLower() == ".pdf")
{
string fn = Guid.NewGuid() + Path.GetExtension(file.FileName);
file.SaveAs(Path.Combine(Server.MapPath("~/assets/pdf/"), fn));
}
if (ext.ToLower() == ".doc" || ext.ToLower() == ".docx")
{
string fn = Guid.NewGuid() + Path.GetExtension(file.FileName);
file.SaveAs(Path.Combine(Server.MapPath("~/assets/doc/"), fn));
}
if (ext.ToLower() == ".xls" || ext.ToLower() == ".xlsx")
{
string fn = Guid.NewGuid() + Path.GetExtension(file.FileName);
file.SaveAs(Path.Combine(Server.MapPath("~/assets/excel/"), fn));
}