检索从ASP.NET页面上传的图像

时间:2010-07-29 13:42:05

标签: asp.net vb.net forms upload

我在一个页面上有一个ASP.NET上传表单,用户可以在其中上传图像。下面是那个代码:

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>

<html>

<head>

<script language="VB" runat="server">

Sub Button1_Click(sender As Object, e As EventArgs)

       If imageupload1.HasFile Then
        imageupload1.SaveAs(Server.MapPath(".") + "/uploadedimages/" & imageupload1.FileName)
           Label1.Text = "Received " & imageupload1.FileName & " Content Type " & imageupload1.PostedFile.ContentType & " Length " & imageupload1.PostedFile.ContentLength
       Else
           Label1.Text = "No uploaded file"
       End If

end sub

</script>

</head>

<body>

<form id="imguplad" runat=server>

<asp:FileUpLoad id="imageupload1" AlternateText="You cannot upload files" runat="server" />
<asp:Button id="Button1" Text="Upload" OnClick="Button1_Click" runat="server" />
<asp:Label id="Label1" runat="server" />
<input type="button" value="Click here when image is uploaded"
onClick="location.href='imageloadtest.aspx';">
</form>

</body>

</html>

然后我尝试使用以下代码在另一页上检索它:

<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
...blah
<body>

<img id="image" src="Server.MapPath(".")+"/uploadedimages/"+Request.Form["imageupload1.FileName"]";">

</body>
...blah

似乎无法让它显示出来?我必须有一些错误的路径。

编辑:顺便说一句,图片上传完美,它不会出现在下一页。

1 个答案:

答案 0 :(得分:0)

尝试

<img id="image" src="<%=Server.MapPath("~")%>/uploadedimages/<%=Request.Form["imageupload1.FileName"]%>">

您还可能需要在会话中手动设置图片名称,因为您无法访问控件imageupload1.FileName不存在的其他页面中的imageupload1

因此,在点击事件中添加Session["ImagePath"] = imageupload1.FileName img代码将Request.Form["imageupload1.FileName"]替换为Session["ImagePath"]