• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Help with loading image file to a specific folder and path to an xml file

Hello,

How do you load an image file to the Application's image folder and copy it's path to an image file? Imager.xml. I am using vb.net programming language with ASP.NET.

<Image_ID>1</Image_ID>
<Image>Image/ImageA.jpg</Image>

Thanks,

Victor
0
vcharles
Asked:
vcharles
  • 8
  • 7
1 Solution
 
Bob LearnedCommented:
I would imagine that you need get to get the inner text value from the <Image> tag, and use Server.MapPath to get the full name, and then process the image.
0
 
vcharlesAuthor Commented:
What control do I use from VS2010 to upload the image?. I have an image control on my form, once they upload the image, I would like them tio see in the image control. How do I achieve this?
0
 
Bob LearnedCommented:
Where is the file located?  
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
vcharlesAuthor Commented:
The users would be uploading the image from their PC to the application's AppData folder.
0
 
Bob LearnedCommented:
Would this name be relative to the AppData folder?

     <Image>Image/ImageA.jpg</Image>

That would be something like this:

Server.MapPath("~/AppData/" + fileName)

Open in new window

0
 
vcharlesAuthor Commented:
Yes, the files will be in the AppData folder, but the XML file will be in the following format:
<Image_ID>1</Image_ID>
<Image>ImageA.jpg</Image>

Once they load the image to the AppData folder, I will need to see it in the Image control in the form, or if possible load it in the Image Viewer before saving it to the AppData folder, which is probably a better approach incase the user doesn't like the way the image looks in the image control.

Thanks,

Victor
0
 
Bob LearnedCommented:
What are you asking for?  Are you asking how to use a FileUpload control?
0
 
vcharlesAuthor Commented:
Yes. and how to view the file in the ASP.NET image control once it's uploaded.
0
 
Bob LearnedCommented:
Take a look here:

FileUpload.HasFile Property
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.hasfile.aspx
<%@ Page Language="VB" %>

<!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>
    <title>FileUpload.SaveAs Method Example</title>
<script runat="server">

      Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        ' Before attempting to save the file, verify
        ' that the FileUpload control contains a file.
        If (FileUpload1.HasFile) Then
          ' Call a helper method routine to save the file.
          SaveFile(FileUpload1.PostedFile)
        Else
          ' Notify the user that a file was not uploaded.
          UploadStatusLabel.Text = "You did not specify a file to upload."
        End If

      End Sub

      Sub SaveFile(ByVal file As HttpPostedFile)

        ' Specify the path to save the uploaded file to.
        Dim savePath As String = "c:\temp\uploads\"

        ' Get the name of the file to upload.
        Dim fileName As String = FileUpload1.FileName

        ' Create the path and file name to check for duplicates.
        Dim pathToCheck As String = savePath + fileName

        ' Create a temporary file name to use for checking duplicates.
        Dim tempfileName As String

        ' Check to see if a file already exists with the
        ' same name as the file to upload.        
        If (System.IO.File.Exists(pathToCheck)) Then
          Dim counter As Integer = 2
          While (System.IO.File.Exists(pathToCheck))
            ' If a file with this name already exists,
            ' prefix the filename with a number.
            tempfileName = counter.ToString() + fileName
            pathToCheck = savePath + tempfileName
            counter = counter + 1
          End While

          fileName = tempfileName

          ' Notify the user that the file name was changed.
          UploadStatusLabel.Text = "A file with the same name already exists." + "<br />" + _
                                   "Your file was saved as " + fileName

        Else

          ' Notify the user that the file was saved successfully.
          UploadStatusLabel.Text = "Your file was uploaded successfully."

        End If

        ' Append the name of the file to upload to the path.
        savePath += fileName

        ' Call the SaveAs method to save the uploaded
        ' file to the specified directory.
        FileUpload1.SaveAs(savePath)

      End Sub

  </script>

</head>
<body>

    <h3>FileUpload.SaveAs Method Example</h3>

    <form id="Form1" runat="server">

        <h4>Select a file to upload:</h4>

        <asp:FileUpload id="FileUpload1"                 
            runat="server">
        </asp:FileUpload>

        <br /><br />

        <asp:Button id="UploadButton" 
            Text="Upload file"
            OnClick="UploadButton_Click"
            runat="server">
        </asp:Button>      

        <hr />

        <asp:Label id="UploadStatusLabel"
            runat="server">
        </asp:Label>   

    </form>

</body>
</html>

Open in new window

0
 
vcharlesAuthor Commented:
Thanks, I will try it and get back to you.
0
 
vcharlesAuthor Commented:
Hi,
I ran the project and tried to upload link.xml to a particular folder but I received the following error:
Could not find a part of the path 'c:\temp\uploads\Link.xml'.
on line:
FileUpload1.SaveAs(savePath)
How do I fix this error?
Thanks,
Victor

0
 
Bob LearnedCommented:
You need rights to write to that folder, since you are running in a partially trusted environment.

FileUpload.SaveAs Method
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas.aspx

For a call to the SaveAs to work, the ASP.NET application must have write access to the directory on the server... You can explicitly grant write access to the account under which the application is running, in the directory in which the uploaded files will be saved.
0
 
vcharlesAuthor Commented:
I gave admin rights on my computer, will chek with my admin. One that is resolved, how do I automaticallt load the image file to an image control on the form?

Thanks,
Victor
0
 
Bob LearnedCommented:
If you have an image file, then you can set the Image.ImageUrl to the image file name.
0
 
vcharlesAuthor Commented:
Got it. Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now