要用FileUpload吧,我写过一段从电脑中选择图片保存的解决方案下面Photos文件夹的代码,asp.net的你参考看看,
前台:
<div id="PhotoInfo">
<ul>
<li>
选择照片:<asp:FileUpload ID="fulPhoto" runat="server" />
</li>
<li>
<asp:Image runat="server" ID="imgPhoto" Height="160px" Width="230px" />
<asp:Button ID="btnUpLoad" runat="server" Text="上传"
onclick="btnUpLoad_Click" /><br />
<asp:Label ID="lblMessage" runat="server" Text="图片上传成功!" ForeColor="Blue" Visible="false"></asp:Label>
</li>
</ul>
</div>
后台:
protected void btnUpLoad_Click(object sender, EventArgs e)
{
string name = this.fulPhoto.FileName;
bool fileOK = false;
if (this.fulPhoto.HasFile)
{
string fileExtension = Path.GetExtension(name).ToLower();
string[] allowExtension = { ".gif", ".jepg", ".jpg", ".png", ".bmp" };
foreach (string f in allowExtension)
{
fileOK = true;
break;
}
}
if (fileOK)
{
string strPath = Server.MapPath(@".\") + "Photos\\";
this.fulPhoto.PostedFile.SaveAs(strPath + name);
this.imgPhoto.ImageUrl = "Photos\\" + name;
this.lblMessage.Visible = true;
//this.fulPhoto.SaveAs(strPath + name);
}
else
{
Response.Write("<script>alert('图片格式不正确!');</script>");
}
}
|