62,040
社区成员
发帖
与我相关
我的任务
分享
public bool SaveCover()
{
String savePath = Server.MapPath("/BookCover/");
if (FileUpload1.HasFile)
{
// Get the name of the file to upload.
String fileName = FileUpload1.FileName;
int fileSize = FileUpload1.PostedFile.ContentLength;
string extension = System.IO.Path.GetExtension(fileName);
if (!(extension == ".jpg" || extension == ".JPG"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('图片格式错误,应为.jpg格式!图书信息保存失败!')", true);
return false;
}
int index = fileName.LastIndexOf(".");
string type = fileName.Substring(index).ToLower();//取文件的扩展名
string newName = System.DateTime.Now.ToString("yyyyMMddHHmmssffff");//声称文件名,防止重复
newName = newName + type;
if (fileSize < 2097152)
{
// Append the name of the file to upload to the path.
savePath += newName;
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user of the name of the file
// was saved under.
//txtCoverPhoto.Text = "/BookCover/" + newName;
CoverPath = "/BookCover/" + newName;
//imgCoverPhoto.ImageUrl = txtCoverPhoto.Text.Trim();
//Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('图书照片上传成功!')", true);
return true;
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('图书照片超过2M,上传失败!图书信息保存失败!')", true);
return false;
}
}
else
{
// Notify the user that a file was not uploaded.
//txtIntro.Text = "You did not specify a file to upload.";
//Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('图书照片上传失败!图书信息保存失败!')", true);
return true ;
}
}