Image1.ImageUrl not rendering image when retrieving from different Directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ahmedhussain
    New Member
    • Dec 2008
    • 79

    Image1.ImageUrl not rendering image when retrieving from different Directory

    Hi,

    I am working on a job portal. I have an Organization picture that I have to Save & Retrieve . My image saving code is working perfectly. But I am dumping the images in my D drive. Now when I use
    Code:
    Image1.ImageUrl = @"D:\DirecotyPath\Image.jpg";
    I does not render an image on the front end...
    What can be done in this situation? And are there any alternatives to that?


    Thanks,

    Regards,

    Syed Ahmed Hussain
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Have you tried fixing the spelling of 'DirectoryPath' ? Maybe you are just going to a folder that doen't exist?

    Do you really have a folder named "DirectoryPath" ? This sounds like a variable that should have been inserted into a string to make the full path.

    Is it reasonable to assume that the viewer of the site doesn't have a D: drive with your photo?

    Comment

    • Ahmedhussain
      New Member
      • Dec 2008
      • 79

      #3
      Have you tried fixing the spelling of 'DirectoryPath' ? Maybe you are just going to a folder that doen't exist?
      What I use here the word " Directory Path" is just a dummy name I am using here.

      Do you really have a folder named "DirectoryPath" ? This sounds like a variable that should have been inserted into a string to make the full path.

      Is it reasonable to assume that the viewer of the site doesn't have a D: drive with your photo?
      No it was just an example (Dummy) name to show that any kind of directory that I wish to open It opens easily and all the paths are stored properly in my database. I use the paths from my DB to retrieve the jpgs from that directory of the given drive ..

      My aim is actually to save the jpgs anywhere on the disk (for right now, later there will be some changes according to the needs) and it simply will store the path in the Database and when you want to retrieve an image, it simply takes the path from the database and then use as the following code line and there are no issue's in the path I have debugged my program several times.

      Code:
      Image1.ImageUrl = imagepath;
      and in image path for example there is a path stored
      @"D:\DirectoryP ath\Gorilla.jpg "

      Now at this point when it render the control, it will show the image control empty.


      Regards,

      Syed Ahmed Hussain

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        The reason this isn't working is because the image is in a directory that the web server doesn't have access to. So if the image control tries to retrieve the image from the server it wont be able to.

        There's a couple of solutions to this problem.
        The first is to move the image into a temporary images folder that is on the web server (say in the website) so that the browser can download it.

        The second is to implement an ASPX page that retrieves the image (as a stream) and writes it to the Response.Output Stream.

        This ASPX page will not return HTML which is what is returned by default..and is expected by the web browser. You need to change the Response.Conten tType to "image/jpg" to indicate to the browser that you are sending an image instead.

        There is no asp code in this page...it's just pure server-side .NET code that retrieves the image (probably based on a parameter that is provided when the page is called) and writes that image to the stream.

        For example, all that would be in your ASPX page would be the following to return the full size image (VB.NET code):
        Code:
         Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
           'Changing the page's content type to indicate the page is returning an image
           Response.ContentType = "image/jpg"
        
           'Retrieving the name of the image 
           Dim imageName = Request.QueryString("imgName")
           Dim path = ""D:\DirectoryPath\"+imgName
        
           If String.IsNullOrEmpty(imageName) = False Then
             'Retrieving the image
             Dim fullSizeImg As System.Drawing.Image
             fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path))
        
             'Writing the image directly to the output stream
             fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg)
        
            'Cleaning up the image
             fullSizeImg.Dispose()
           End If
        End Sub
        This code would be in a page called, say, GetImage.aspx ....

        Now, when you specify the image source for the ImageControl you use "GetImage.aspx? imgName=Gorilla .jpg"

        For example, in your page you would have:
        Code:
        <asp:Image ID="Image1" ImageUrl = "GetImage.aspx?imgName=Gorilla.jpg" />

        -Frinny

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          And I repeat
          Originally posted by tlhIn'toq
          Is it reasonable to assume that the viewer of the site doesn't have a D: drive with your photo?
          Meaning that me, sitting at my desk at my house looking at your site... I don't have 'your' D: drive with your D:\\DirectoryPa th\Image.jpg on it - now do I?

          Frinny you really do have much more patience than I do. It was terribly sweet of you to provide all the code to the OP

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            No guarantees that it's going to work as is ;)

            (but then again it looks right to me)

            Comment

            • Ahmedhussain
              New Member
              • Dec 2008
              • 79

              #7
              @tlhintoq :

              You needed patience for what ??? :D

              After all you both are the moderators, someone gotta be more patient then I thought one should be :) ...

              No you dont have D Drive, but again, I am saving it on my Server, right here on my laptop!!!!

              I am not deploying my site right now....Moreover wont it be saving the images on the server???

              Comment

              • Ahmedhussain
                New Member
                • Dec 2008
                • 79

                #8
                Server, means Client-Server Architecture??? ? I think you remember that???

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  When I was referring to the web server I was referring to IIS....
                  You may consider your laptop as the server computer; but IIS doesn't have access to everything on the laptop.

                  It all depends on how you set up your website in IIS....but essentially when the browser requests some resource (a web page, an image, a JavaScript file...) the server has to return it. The only way that a browser can do this is using a URI. If the browser makes a request for "D:\somePath\so mething.xyz" then the server won't know how to translate it to retrieve the resource that you are looking for....never mind that you aren't even going to be able to make a request to the server using "D:\somePath\so mething.xyz"... the browser's just going to bock at this.

                  So you either need to copy/move/put the resource somewhere in your website so that you can create a URI/URL to the resource that the browser needs, or you need to create something (an ASPX page for example) that is accessible by the browser that will return the resource requested.

                  If the resource is on a different drive...you can't really specify a URL to that.

                  Therefore, I recommended that you create an ASPX page that retrieves the resources and writes it to the browser. This ASPX page is accessible by URL so that the browser can request the resource that is not "on the server" or "in the website".

                  -Frinny

                  Comment

                  • Ahmedhussain
                    New Member
                    • Dec 2008
                    • 79

                    #10
                    Frinny :

                    Thanks, I am learning so much from you guys :)

                    Regards,

                    Syed Ahmed Hussain

                    Comment

                    • GolferGirly
                      New Member
                      • Jul 2016
                      • 1

                      #11
                      I found a minor error in line 8:
                      Code:
                      Dim path = ""D:\DirectoryPath\"+imgName
                      where aspx image control property imgName should be the code behind variable imageName or some string literal.
                      Corrected:
                      Code:
                      Dim path = ""D:\DirectoryPath\"+imageName
                      Last edited by GolferGirly; Jul 26 '16, 11:16 PM. Reason: forgot to show correct code with the incorrect code

                      Comment

                      Working...