views:

25

answers:

1

Hey guys,

I've got an ASP.NET upload form on one page, where the user can upload an image. heres the code for that one:

<%@ 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>

Then I am trying to retrieve it on another page, using the following code:

<%@ 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

Can't seem to get it to show up? I must have some path slightly wrong. Any ideas?

Thanks

EDIT: By the way, the image uploads perfectly, it just won't show up on the next page.

A: 

Try with

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

You also may have to set manually the image name in session because you won't have access to imageupload1.FileName in another page, where the control imageupload1 doesn't exist.

So in your click event add Session["ImagePath"] = imageupload1.FileName And the img tag replace Request.Form["imageupload1.FileName"] by Session["ImagePath"]

Julien N
no luck with that. im using a "." throughout the whole project, so that should be correct anyway? thanks
IceDragon
hum sorry, I changed my mind few seconds after... I think there is a mismatch with how the code should be used in aspx page... So I updated the full `img` tag.
Julien N
that doesn't seem to want to work either, now it just doesnt display anything (even the broken picture icon). thanks
IceDragon
Huuum probably because the imageupload1 doesn't exist on the page. Try with the change I added : setting the filename in session.
Julien N
And you should view the source of the page to check what path is written in the `img` tag. It may help to understand what is wrong.
Julien N
where abouts in the click event would the Session["ImagePath"] = imageupload1.FileName go? thanks
IceDragon
Almost anywhere, but after `imageupload1.SaveAs(..)` would probably be the best place
Julien N