[英]Image is NULL when I try to upload image ASP.NET
代码:
string FileName = Path.GetFileNameWithoutExtension(customerImage.ImageFile.FileName);
string FileExtension = Path.GetExtension(customerImage.ImageFile.FileName);
FileName = DateTime.Now.ToString("yyyyMMdd") + "-" + FileName.Trim() + FileExtension;
string UploadPath = ConfigurationManager.AppSettings["UserImagePath"].ToString();
customerImage.Image = UploadPath + FileName;
customerImage.ImageFile.SaveAs(customerImage.Image);
有问题的线路:
string FileName = Path.GetFileNameWithoutExtension(customerImage.ImageFile.FileName);
错误:上传图片错误
请帮助我是一名大学生,这是一个最终项目:((((((((((()
您有一个 object 引用错误(空引用异常),很可能是文件上传控件没有正确初始化或者在向服务器端发送请求时没有文件附加到控件。
如果 customerImage 是 ASP 文件上传控件,您可以访问文件名,如下所示:
FileUpload1.FileName
请参阅以下示例:对于 HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" onclick="Button1_Click"/>
<br />
<asp:Image ID="Image1" runat="server" />
服务器端
protected void Button1_Click(object sender, EventArgs e) {
StartUpLoad();
}
private void StartUpLoad() {
//get the file name of the posted image
string imgName = FileUpload1.FileName;
//sets the image path
string imgPath = "ImageStorage/" + imgName;
//get the size in bytes that
int imgSize = FileUpload1.PostedFile.ContentLength;
//validates the posted file before saving
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "") {
// 10240 KB means 10MB, You can change the value based on your requirement
if (FileUpload1.PostedFile.ContentLength > 10240) {
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);
} else {
//then save it to the Folder
FileUpload1.SaveAs(Server.MapPath(imgPath));
Image1.ImageUrl = "~/" + imgPath;
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);
}
}
}
试试这个,看看你的代码有什么问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.