[英]Image is NULL when I try to upload image ASP.NET
Code:代码:
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);
Problematic line:有问题的线路:
string FileName = Path.GetFileNameWithoutExtension(customerImage.ImageFile.FileName);
Error: UPLOAD IMAGE ERROR错误:上传图片错误
PLEASE HELP IM A COLLEGE STUDENT AND THIS IS FOR A FINAL PROJECT:((((((((((请帮助我是一名大学生,这是一个最终项目:((((((((((()
You have a object reference error (Null Reference Exception),most probably the file upload control has not been initialized properly or there is not file attached to the control when sending the request to server side.您有一个 object 引用错误(空引用异常),很可能是文件上传控件没有正确初始化或者在向服务器端发送请求时没有文件附加到控件。
If customerImage is a ASP File Upload control you can access the file name like below:如果 customerImage 是 ASP 文件上传控件,您可以访问文件名,如下所示:
FileUpload1.FileName
Pleas see below sample: For the HTML请参阅以下示例:对于 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" />
For Server Side服务器端
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);
}
}
}
Try this and see what is wrong with your code.试试这个,看看你的代码有什么问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.