上传图片代码。
前台是一个upload控件加一个按钮:
addline.aspx:
addline.cs:
addline.aspx:
<tr>
<td style="width: 30px"> <asp:Label ID="Label9" runat="server" Text="Picture URL:"></asp:Label></td>
<td> <asp:TextBox ID="imgurl"
runat="server">../img/Noimg.jpg</asp:TextBox>
<asp:Label ID="addimg" runat="server" ForeColor="Red"></asp:Label><br />
<asp:Label ID="Label10" runat="server" Text="Choose PIC:"></asp:Label>
<asp:FileUpload ID="imgurl1" runat="server" />
<asp:Button
ID="Button1" runat="server" OnClick="Button1_Click" Text="upload" /></td>
</tr>
<td style="width: 30px"> <asp:Label ID="Label9" runat="server" Text="Picture URL:"></asp:Label></td>
<td> <asp:TextBox ID="imgurl"
runat="server">../img/Noimg.jpg</asp:TextBox>
<asp:Label ID="addimg" runat="server" ForeColor="Red"></asp:Label><br />
<asp:Label ID="Label10" runat="server" Text="Choose PIC:"></asp:Label>
<asp:FileUpload ID="imgurl1" runat="server" />
<asp:Button
ID="Button1" runat="server" OnClick="Button1_Click" Text="upload" /></td>
</tr>
addline.cs:
protected void Button1_Click(object sender, EventArgs e)
{
Boolean fileOK = false;
String path = Server.MapPath("~/img/tour/");
String url = "../img/tour/" + imgurl1.FileName;
if (imgurl1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(imgurl1.FileName).ToLower();
String[] allowedExtensions =
{ ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
imgurl1.PostedFile.SaveAs(path
+ imgurl1.FileName);
addimg.Text = "File uploaded!";
imgurl.Text = url;
}
else
{
addimg.Text = "Cannot accept files of this type.";
}
}
{
Boolean fileOK = false;
String path = Server.MapPath("~/img/tour/");
String url = "../img/tour/" + imgurl1.FileName;
if (imgurl1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(imgurl1.FileName).ToLower();
String[] allowedExtensions =
{ ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
imgurl1.PostedFile.SaveAs(path
+ imgurl1.FileName);
addimg.Text = "File uploaded!";
imgurl.Text = url;
}
else
{
addimg.Text = "Cannot accept files of this type.";
}
}