FileUpload控件的使用
下面的代码是从本地上传图片,并显示出来。 我觉得代码没什么问题啊,可就是显示不出来!请帮忙看一下,指点一下,不胜感激!public partial class q : System.Web.UI.Page
{
Boolean fileok = false;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(fileok);
if (FileUpload1.HasFile)
{
string fileext = (FileUpload1.FileName).ToLower();
string[] allowedextensions ={ ".jpg", ".bmp", ".gif", ".bng" };
for (int i = 0; i < allowedextensions.Length; i++)
{
if (fileext == allowedextensions[i])
{
fileok = true;
}
}
}
if (FileUpload1.PostedFile.ContentLength > 1024000)
{
fileok = false;
}
if (fileok)
{
try
{ FileUpload1.PostedFile.SaveAs(MapPath ("file/") + FileUpload1.PostedFile.FileName);
Label1.Text = "上传文件成功";
Image1.ImageUrl = MapPath ("file/") + FileUpload1.PostedFile.FileName;
}
catch
{
Label1.Text = "无法上传图片";
}
}
else
{
Label1.Text = "文件类型不对或文件太大!";
}
}
}