Hi, I have a site that allows users to upload images. I am able to upload image, save to server, and display images right away. Problem is that the images dissapear after I navigate away from the site! I am using the file system to store images. You can view the site and upload a picture at: http://knudmt.brinkster.net/Inventory.aspx

code-behind is as follows:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Inventory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //get the path
        getPath();
        //file name to string
        String img = FileUpload1.FileName.ToString();
        //save file for(test)
        FileUpload1.SaveAs("C:\\Users\\Matthew\\Documents\\Visual Studio 2005\\WebSites\\WebSite6\\ImageUpload\\" + img);
        //offical
        //FileUpload1.SaveAs("E:\\Sites\\Single5\\knudmt\\webroot\\ImageUpload\\" + img);
        Image1.Visible = true;
        Image1.ImageUrl = "~/ImageUpload/" + img;
        FileUpload1.Visible = false;
        
    }
    protected string getPath()
    {
       string path = Server.MapPath("Inventory.aspx");
       return path;
    }

my .aspx page code is as follows, you will see where I attempted to use javascript to solve this issue.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Inventory.aspx.cs" Inherits="Inventory" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script language=javascript type="text/javascript">
        function setUrl(){
           
            //document.getElementById('tryImg').src = "~/Images/logoChoice.jpg";
            //document.getElementById('tryImg').src = "http://localhost
            
        }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table align="center" style="width: 800px">
            <tr>
                <td colspan="3" style="font-size: 23pt; color: #ffffff; font-family: 'Segoe Script';
                    text-align: left">
                    <img src="Images/logoChoice.jpg" style="width: 240px; height: 104px; text-align: left" /></td>
            </tr>
            <tr>
                <td colspan="3" style="font-size: 15pt; color: #ffffff; font-family: 'Segoe Script';
                    height: 21px; background-color: #330033; text-align: center">
                    About Us</td>
            </tr>
        </table>
    
    </div>
        <table align="center" style="width: 800px">
            <tr>
                <td colspan="6" style="font-family: 'Segoe Script'; height: 21px; background-color: #669900">
                    &nbsp; &nbsp;
                    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/index.aspx">Home</asp:HyperLink>
                    &nbsp; &nbsp; &nbsp; &nbsp;<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/About.aspx">About Us</asp:HyperLink>
                    &nbsp; &nbsp; &nbsp; &nbsp;<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/location.aspx">Location</asp:HyperLink>
                    &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;<asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/contact.aspx">Contact Us</asp:HyperLink>
                    &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
                    <asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="~/Inventory.aspx">Inventory</asp:HyperLink>
                    &nbsp; &nbsp; &nbsp; &nbsp;
                    <asp:HyperLink ID="HyperLink6" runat="server" Style="text-align: right">Puppy Parade</asp:HyperLink></td>
            </tr>
        </table>
        <br />
        <br />
        <table align="center" style="width: 800px">
            <tr>
                <td>
                    <asp:Image ID="Image1" runat="server" Visible="False" ImageUrl="setUrl();" Width="144px" />
                    <img name="tryImg" id="tryImg" alt="" src="" visible="true" runat="server" style="width: 144px" /></td>
                <td>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></td>
                <td>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Image ID="Image2" runat="server" Visible="False" Width="144px" /></td>
                <td>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:FileUpload ID="FileUpload2" runat="server" />
                    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" /></td>
                <td>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Image ID="Image3" runat="server" Visible="False" Width="144px" /></td>
                <td>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:FileUpload ID="FileUpload3" runat="server" />
                    <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Button" /></td>
                <td>
                </td>
                <td>
                </td>
            </tr>
        </table>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <table align="center" style="width: 800px">
            <tr>
                <td colspan="3" style="font-weight: bold; font-size: 13pt; color: #ffffff; font-family: 'Segoe Script';
                    height: 25px; background-color: #669900; text-align: center">
                    Tucci's</td>
            </tr>
            <tr>
                <td colspan="3" style="font-weight: bold; font-size: 15pt; color: #ffffff; font-family: 'Segoe Script';
                    height: 10px; background-color: #330033; text-align: center">
                    Consignment Furniture</td>
            </tr>
            <tr>
                <td colspan="3">
                    Tucci's Unique Furnishings<br />
                    1155 Barret Parkway<br />
                    Kennesaw GA, 30144<br />
                    Phone: (770) 296-5888<br />
                    Fax: (770) 783-8390</td>
            </tr>
        </table>
    </form>
</body>
</html>

Recommended Answers

All 8 Replies

Root operator ~ cannot be used with html tags & javascript.

I think you need to give relative path like "~/ImageUpload/". so u no need to even user server.mapPath.

That is right you can't use operator "~" in javascript use operator "."
Try it out

// Code Behind .......(.cs File)

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Demo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath("~/Images/") + FileUpload1.FileName);
            Image1.ImageUrl = "http://" + System.Web.HttpContext.Current.Request.Url.Host + System.Web.HttpContext.Current.Request.ApplicationPath +"/Images/" + FileUpload1.FileName.ToString();
        }
    }
}

// Design Page....(.aspx)

<form id="form1" runat="server">
    <div>
    
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/Autumn Leaves.jpg" 
            Width="100px" />
    
    </div>
    </form>
commented: 44th post without code tags. -1

i've seen your website.....i like your font..

Thanks! I am a horrible web designer! I am an I.T. student but really enjoy website creation. I just wish I had more knowledge.

I have not tried this yet but I will. Thank you for taking the time to post a reply! Do I have to set the image url in the tag? or can I leave it null? I suppose I could always just have a blank.jpg in the image folder. Thank you for your help! I will try this out today!

I tried the code provided. But I have VS 2005. So I cant use the namespace System.Linq or System.Web.Linq. Anyway to do this without these name spaces?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.