Save fileuplaod path into xml and get it out.

Last post 07-07-2008 1:40 PM by FarhanK. 7 replies.

Sort Posts:

  • Save fileuplaod path into xml and get it out.

    07-03-2008, 10:52 AM
    • Loading...
    • Frozzare
    • Joined on 08-23-2007, 7:28 PM
    • Sweden
    • Posts 70

     Hello.

    I have my fileupload control, who save 

    FileUpload1.PostedFile.SaveAs(Server.MapPath("upload\\images\\" + StrNow + "_" + StrFileName));

    in folder, upload\images\, 

     when i write the code to save images path into xml data. like this, server.InnerText = this.FileUpload1.PostedFile.FileName; but it write out the name of the uploade file, not the saved file. the save filed are like this: 36463456363_filename.jpg,and this write out just filename.jpg. 

    But i want it to write out the path to images, like, \upload\images\36463456363_filename.jpg

    <images>
      <imgsrc> \upload\images\36463456363_filename.jpg</imgsrc>
    </images>

    and then on images.aspx site, i want to get out the path to the images like, images.aspx?path=\upload\images\36463456363_filename.jpg, and the images.aspx site show that images.

     

    I hope you get it, sorry for bad english

  • Re: Save fileuplaod path into xml and get it out.

    07-06-2008, 2:53 AM
    Answer
    • Loading...
    • FarhanK
    • Joined on 01-14-2008, 12:21 PM
    • Posts 240

    Hi,

    I've created an small example for you. It's uploading images to the Images folder and after uploading it saves the uploaded path in Images.xml (xml file) which is in the root directory. Repeater is used to show the images saved (Dataset is obtained from xml file). There is no content type check I hope you can put it there yourself.

    ------------------ HTML

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            [Upload Images]<br />
            <asp:FileUpload ID="FileUpload1" runat="server" /><br />
            <asp:Button ID="BtnUpload" Text=" Upload Image " runat="server" OnClick="BtnUpload_Click" />
            <br />
            <br />
            <asp:Repeater ID="RptImages" runat="server">
                <HeaderTemplate>
                    <table width="100" align="center">
                </HeaderTemplate>
                <ItemTemplate>
                    <tr><td><asp:Image ID="Img" ImageUrl='<%# Eval("path") %>' runat="server" /></td></tr>
                </ItemTemplate>
                <AlternatingItemTemplate>
                    <tr><td><asp:Image ID="Img" ImageUrl='<%# Eval("path") %>' runat="server" /></td></tr>
                </AlternatingItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
        </div>
        </form>
    </body>
    </html>

    ----------------- Code
    Imports System
    Imports System.Data
    Imports System.Configuration
    Imports System.Collections
    Imports System.Web
    Imports System.Web.Security
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.HtmlControls
    Imports System.IO
    Imports System.Xml


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            BindRepeater()
        End If
    End Sub

    Protected Sub BtnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnUpload.Click
        Dim Now As String = DateTime.Now.Ticks.ToString()
        Dim FileName As String = FileUpload1.PostedFile.FileName
        Dim FullPath As String = "Images/" + Now + "_" + Path.GetFileName(FileName)
       
        If FileUpload1.PostedFile.ContentLength > 0 Then
            FileUpload1.PostedFile.SaveAs(Server.MapPath(FullPath))
            SaveImagePathToXml(FullPath)
        End If
    End Sub

    ''' <summary>
    ''' Writes uploaded image path to xml file
    ''' </summary>
    ''' <param name="path"></param>
    Private Sub SaveImagePathToXml(ByVal path As String)
        Dim ImageFileName As String = "Images.xml"
        ' Create xml file if not created.
        If Not File.Exists(Server.MapPath(ImageFileName)) Then
            Dim Fs As New FileStream(Server.MapPath("Images.xml"), FileMode.Create)
            Dim Sw As New StreamWriter(Fs)
            Sw.Write("<data></data>")
            Sw.Flush()
            Sw.Dispose()
            Fs.Dispose()
        End If
        ' write path to xml file.
        Dim Document As New XmlDocument()
        Document.Load(Server.MapPath(ImageFileName))
       
        Dim Xm As XmlElement = Document.CreateElement("image")
        Dim Xa As XmlAttribute = Document.CreateAttribute("path")
        Xa.Value = path
        Xm.Attributes.Append(Xa)
        Document.ChildNodes(0).AppendChild(Xm)
        Document.Save(Server.MapPath("Images.xml"))
        BindRepeater()
    End Sub

    Private Sub BindRepeater()

        If File.Exists(Server.MapPath("Images.xml")) Then
            Dim Ds As New DataSet()
            Ds.ReadXml(Server.MapPath("Images.xml"))
            RptImages.DataSource = Ds
            RptImages.DataBind()
        End If
    End Sub

    [Please mark the post as answer that helps you.]

    Regards,
    Farhan Uddin Khan
    Enpointe Technologies
  • Re: Save fileuplaod path into xml and get it out.

    07-07-2008, 5:40 AM
    • Loading...
    • Frozzare
    • Joined on 08-23-2007, 7:28 PM
    • Sweden
    • Posts 70

    Just one problem, when i convert the code to C#, so din't this line work,

    Line 79:         Document.ChildNodes(0).AppendChild(Xm);

    You can not use the non-anropningsbara member System.Xml.XmlNode.ChildNodes as a method.

     
     

  • Re: Save fileuplaod path into xml and get it out.

    07-07-2008, 7:28 AM
    • Loading...
    • Pushkar
    • Joined on 02-17-2006, 7:27 AM
    • http://www.plumtreegroup.net CDIPL - AHD
    • Posts 747

    Try this one. 

    Document.ChildNodes[0].AppendChild(Xm); 

    "A conclusion is where you got tired of thinking.
    Be different. Think "

    Remember to click “Mark as Answer” on the post If you get answer from my post(s) !

    Thanks Guys
    ------------
    Pushkar
  • Re: Save fileuplaod path into xml and get it out.

    07-07-2008, 8:19 AM
    • Loading...
    • Frozzare
    • Joined on 08-23-2007, 7:28 PM
    • Sweden
    • Posts 70

    Thanks it work, but the asp:Repeater don't show any images when the images is upload ;S

     my C# code, yes have "using:" all the field how need.

     

    protected void Page_Load(object sender, EventArgs e)
        {
                 if (!Page.IsPostBack) {
             BindRepeater();
        }
        }
    
        protected void BtnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile.FileName != "")
            {
    
                string StrFileName = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\") + 1);
                string StrFileType = FileUpload1.PostedFile.ContentType;
                string StrNow = Convert.ToString(DateTime.Now.Ticks);
                string FullPath = "upload\\images\\" + StrNow + "_" + Path.GetFileName(StrFileName);
    
                int fileSize = (int)Math.Round((double)FileUpload1.PostedFile.ContentLength / 1024, 0);
                int maxSize = 1024;
    
                if (fileSize > maxSize)
                    ErrorLabel1.Text = Convert.ToString(" <font color='Red' size='2'>Uploade faild, just 1mb big.</font>");
          
                else
                {
    
                    FileUpload1.PostedFile.SaveAs(Server.MapPath(FullPath));
                    SaveImagePathToXml(FullPath);
    
                    ErrorLabel1.Text = Convert.ToString(" <font color='Green' size='2'>Your images has been upload!</font>");
                
    
    
                }
            }
            else
            {
                ErrorLabel1.Text = Convert.ToString(" <font color='Red' size='2'>Upploade faild, you have to have a images.</font>");
            }
    
        }
    
        private void SaveImagePathToXml(string path)
        {
            string ImageFileName = "App_Data\\Images.xml";
            // Create xml file if not created.
            if (!File.Exists(Server.MapPath(ImageFileName)))
            {
                FileStream Fs = new FileStream(Server.MapPath("App_Data\\Images.xml"), FileMode.Create);
                StreamWriter Sw = new StreamWriter(Fs);
                Sw.Write("&lt;data></data>");
                Sw.Flush();
                Sw.Dispose();
                Fs.Dispose();
            }
            // write path to xml file.
            XmlDocument Document = new XmlDocument();
            Document.Load(Server.MapPath(ImageFileName));
    
            XmlElement Xm = Document.CreateElement("image");
            XmlAttribute Xa = Document.CreateAttribute("path");
            Xa.Value = path;
            Xm.Attributes.Append(Xa);
     Document.ChildNodes[0].AppendChild(Xm); 
            Document.Save(Server.MapPath("App_Data\\Images.xml"));
            BindRepeater();
        }
    
        private void BindRepeater()
        {
    
            if (File.Exists(Server.MapPath("App_Data\\Images.xml")))
            {
                DataSet Ds = new DataSet();
                Ds.ReadXml(Server.MapPath("App_Data\\Images.xml"));
                RptImages.DataSource = Ds;
                RptImages.DataBind();
            }
        }
      
  • Re: Save fileuplaod path into xml and get it out.

    07-07-2008, 12:15 PM
    • Loading...
    • FarhanK
    • Joined on 01-14-2008, 12:21 PM
    • Posts 240

     Hi,
     I used your code and its working fine. I just replaced the \\ with / in the path. Please also see the HTML. Hope this works for you. Take care
     
     
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Label ID="ErrorLabel1" runat="server"></asp:Label><br />
            [Upload Images]<br />
            <asp:FileUpload ID="FileUpload1" runat="server" /><br />
            <asp:Button ID="BtnUpload" Text=" Upload Image " runat="server" OnClick="BtnUpload_Click" />
            <br />
            <br />
            <asp:Repeater ID="RptImages" runat="server">
                <HeaderTemplate>
                    <table width="100" align="center">
                </HeaderTemplate>
                <ItemTemplate>
                    <tr><td><asp:Image ID="Img" ImageUrl='<%# Eval("path") %>' runat="server" /></td></tr>
                </ItemTemplate>
                <AlternatingItemTemplate>
                    <tr><td><asp:Image ID="Img" ImageUrl='<%# Eval("path") %>' runat="server" /></td></tr>
                </AlternatingItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
        </div>
        </form>
    </body>
    </html>   
       

    // Code
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindRepeater();
            }
        }

        protected void BtnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile.FileName != "")
            {

                string StrFileName = FileUpload1.PostedFile.FileName;
                string StrFileType = FileUpload1.PostedFile.ContentType;
                string StrNow = Convert.ToString(DateTime.Now.Ticks);
                string FullPath = "upload/images/" + StrNow + "_" + Path.GetFileName(StrFileName);

                int fileSize = (int)Math.Round((double)FileUpload1.PostedFile.ContentLength / 1024, 0);
                int maxSize = 1024;

                if (fileSize > maxSize)
                    ErrorLabel1.Text = Convert.ToString(" <font color='Red' size='2'>Uploade faild, just 1mb big.</font>");

                else
                {

                    FileUpload1.PostedFile.SaveAs(Server.MapPath(FullPath));
                    SaveImagePathToXml(FullPath);

                    ErrorLabel1.Text = Convert.ToString(" <font color='Green' size='2'>Your images has been upload!</font>");
                }
            }
            else
            {
                ErrorLabel1.Text = Convert.ToString(" <font color='Red' size='2'>Upploade faild, you have to have a images.</font>");
            }

        }

        private void SaveImagePathToXml(string path)
        {
            string ImageFileName = "App_Data/Images.xml";
            // Create xml file if not created.
            if (!File.Exists(Server.MapPath(ImageFileName)))
            {
                FileStream Fs = new FileStream(Server.MapPath("App_Data/Images.xml"), FileMode.Create);
                StreamWriter Sw = new StreamWriter(Fs);
                Sw.Write("<data></data>");
                Sw.Flush();
                Sw.Dispose();
                Fs.Dispose();
            }
            // write path to xml file.
            XmlDocument Document = new XmlDocument();
            Document.Load(Server.MapPath(ImageFileName));

            XmlElement Xm = Document.CreateElement("image");
            XmlAttribute Xa = Document.CreateAttribute("path");
            Xa.Value = path;
            Xm.Attributes.Append(Xa);
            Document.ChildNodes[0].AppendChild(Xm);
            Document.Save(Server.MapPath("App_Data/Images.xml"));
            BindRepeater();
        }

        private void BindRepeater()
        {

            if (File.Exists(Server.MapPath("App_Data/Images.xml")))
            {
                DataSet Ds = new DataSet();
                Ds.ReadXml(Server.MapPath("App_Data/Images.xml"));
                RptImages.DataSource = Ds;
                RptImages.DataBind();
            }
        }

    [Please mark the post as answer that helps you.]

    Regards,
    Farhan Uddin Khan
    Enpointe Technologies
  • Re: Save fileuplaod path into xml and get it out.

    07-07-2008, 1:18 PM
    • Loading...
    • Frozzare
    • Joined on 08-23-2007, 7:28 PM
    • Sweden
    • Posts 70

     thanks, it work, but it show all the images, i just want it to show the images how has bean upload precisely, and i wan't it to show like this:

    Default.asxp = Upload

    Images.aspx?path=/upload/images/file.jpg

    and then show the images there?

  • Re: Save fileuplaod path into xml and get it out.

    07-07-2008, 1:40 PM
    • Loading...
    • FarhanK
    • Joined on 01-14-2008, 12:21 PM
    • Posts 240

    HI,

    I am sorry I did not understand it clearly. Do you want to show the image that has been uploaded by the user on a pop up window? If this isn't right then please can you explain a bit more about your requirement.

    Take care.

    [Please mark the post as answer that helps you.]

    Regards,
    Farhan Uddin Khan
    Enpointe Technologies
Page 1 of 1 (8 items)
Microsoft Communities
Page view counter