我创建了一个用户控件,如下图所示。在它背后的代码我有保存图像的方法。
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UCImageUploader.ascx.cs"
Inherits="WebApplicationFor_ABC.UCImageUploader" %>
<div>
<asp:FileUpload ID="FileUploadImage" runat="server" />
<asp:Image ID="ImageThumbnail" runat="server" />
<asp:Label ID="LblMsg" runat="server" Text=""></asp:Label>
<asp:CheckBox ID="CheckBoxDelete" runat="server" Text="Delete"/>
</div>
后面的代码如下
public partial class UCImageUploader : System.Web.UI.UserControl
{
public bool SaveImg()
{
//file field isn't empty
if (FileUploadImage.PostedFile != null)
{
//check file size
HttpPostedFile img = FileUploadImage.PostedFile;
if (img.ContentLength == 0) // no file uploaded
{
msg = "No file was uploaded";
LblMsg.Text = msg;
return false;
}
else if (img.ContentLength > fileSizeInKB * 1024) // file size exceeds limit
{
msg = "File size exceeds the limit. Max is " + fileSizeInKB + "KB";
LblMsg.Text = msg;
return false;
}
else // accepted file size
{
//check file extension jpg,jpeg,BMP
string[] fileExtensions = { "jpg", "jpeg", "bmp" };
if (!fileExtensions.Contains(Path.GetExtension(img.FileName).ToLower()))
{
msg = "File extension must be of jpg,jpeg,bmp";
LblMsg.Text = msg;
return false;
}
}
}
msg = "Valid File";
LblMsg.Text = msg;
return valid;
}
在另一个按钮的点击事件中,我想调用此SaveImg()
方法并保存图像。问题是因为回发图像返回为null。我怎样才能解决这个问题。
答案 0 :(得分:1)
要访问图像,请不要通过文件上传控件执行此操作,例如
FileUploadImage.SaveAs(savePath);