Solved

Image refresh file IO problem or cache or something else?

Posted on 2008-06-11
11
352 Views
Last Modified: 2013-11-07
I've read the topic http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23066709.html but my problem isn't solved with it.

Is there any way to detect if file IO is completed?.

I have the same problem.  I choose first a year and then I see a gridview with all the pictures of persons in that year.  I select a person and click a browse button, choose my file and upload my file by clicking a button.  The code behind the file uploads the file and then refreshes my gridview (gridview.databind) and in this gridview I see then the old picture.  If I hit F5 of my browser then I see the new uploaded picture.

When there is no original picture available for a person I display a "No image available"-image. when I upload a picture for such person without original picture and when the gridview refreshes, the new picture is there.  So I guess it couldn't be a file IO completion problem?

Response.redirect is not an option for me because then my page loads from the beginning.  Before I see my gridview I need to choose first a "Year" from a dropdownlist

Then I thought it could be a cache problem, but after reading and testing all suggestions from http://www.syncfusion.com/FAQ/aspnet/web_c25c.aspx I'm not sure, because when i do the following:
********
Dim objItem As DictionaryEntry
For Each objItem In Cache
Response.Write (objItem.Key.ToString ())
Next  
********

I don't get anything on my screen, which looks like there is nohting in the cache.

What else could it be then?

Kind regards
0
Comment
Question by:DataconsultGent
  • 5
  • 4
  • 2
11 Comments
 
LVL 12

Expert Comment

by:renjurdevan
ID: 21758034
Disable cache !!

Add following snippen it Page_Load

Response.Cache.SetCacheability(HttpCacheability.NoCache)
0
 

Author Comment

by:DataconsultGent
ID: 21758142
Dear renjurdevan,

This doesn't help.  I don't see my new uploaded picture in my gridview.
Attached is my code on page load and when the button is clicked

I upload the original picture of the person, I save them twice (small (width=68 for 96dpi) and big (width=223 fpr 300dpi)) and then I delete the original uploade file, after that I refresh my gridview

I've also added my code on Gridview_RowDatabound
    Protected Sub UploadClick(ByVal sender As Object, ByVal e As System.EventArgs)

        If DdlStudent.SelectedItem.Value <> 0 Then

            'Variabele voor bestandslocatie

            Dim strPath As String = "E:\Websites\ryedb.uploads\images\in\" & Profile.District & "\"

            Dim strUploadedFile, strUploadedFileExtensie, strFile, strFullPath, strFullPathNewSmall, strFullPathNewLarge As String

            Dim canvas As New Canvas()

            Dim graph As New Graphic()

            Dim rect As New XRect()
 

            'Bestandsnaam en extensie van het opgeladen bestand

            strUploadedFile = FileUpload1.PostedFile.FileName

            strUploadedFileExtensie = Right(strUploadedFile, 3)
 

            'District uit Profile halen => nodig om bestand een naam te geven

            Dim strDistrict As String = Profile.District
 

            'Nieuwe bestandsnaam

            strFile = strDistrict & "_in_" & DdlStudent.SelectedItem.Value 'Idin student uit dropdownlijst toevoegen

            strFullPath = strPath & strFile & "." & strUploadedFileExtensie

            strFullPathNewSmall = strPath & strFile & "s.jpg"

            strFullPathNewLarge = strPath & strFile & "l.jpg"

            Try

                If UCase(strUploadedFileExtensie) = "JPG" Or UCase(strUploadedFileExtensie) = "BMP" Or UCase(strUploadedFileExtensie) = "EPS" Or UCase(strUploadedFileExtensie) = "GIF" Or UCase(strUploadedFileExtensie) = "TIF" Then

                    FileUpload1.PostedFile.SaveAs(strFullPath)
 

                    '1) Foto verkleinen naar breedte = 68 en in 96dpi

                    graph.SetFile(strFullPath)

                    rect.String = graph(1).Rectangle

                    canvas.Clear()

                    canvas.Width = 68
 

                    canvas.Height = (68 * rect.Height) / rect.Width

                    canvas.DrawFile(strFullPath, "size=" & canvas.Width & "," & canvas.Height)

                    canvas.SaveAs(strFullPathNewSmall, "Quality=high HRes=96 VRes=96")
 

                    '2) Foto verkleinen naar breedte = 213 en opslaan in 300dpi

                    graph.SetFile(strFullPath)

                    rect.String = graph(1).Rectangle

                    canvas.Clear()

                    canvas.Width = 213
 

                    canvas.Height = (213 * rect.Height) / rect.Width

                    canvas.DrawFile(strFullPath, "size=" & canvas.Width & "," & canvas.Height)

                    canvas.SaveAs(strFullPathNewLarge, "Quality=high HRes=300 VRes=300")

                    File.Delete(strFullPath)    'Delete upgeloade file
 

                    lblResults.Text = "Your file is uploaded.<br/>Due to webbrowser caching it's possible that you don't see your new uploaded picture. After hitting refresh (F5 for Internet Explorer) you should see your new uploaded picture.<br/>"

                    lblError.Text = ""

                Else

                    lblError.Text = "Your file couldn't be uploaded for the following reason: Your file is not a .jpg, .bmp, .eps, .gif or .tif file."

                    lblResults.Text = ""

                End If

            Catch Ex As Exception

                lblError.Text = "Your file couldn't be upload for the following reasen: " & Ex.Message

                lblResults.Text = ""

            Finally

                'Refresh gridview

                GridView1.DataBind()

            End Try

        Else

            lblError.Text = "Please select first a student."

            lblResults.Text = ""

        End If

    End Sub
 

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

        'Check if file exists

        Dim strFile As String = "E:\Websites\ryedb.uploads\images\" 'Standaard path

        Dim photo As System.Web.UI.WebControls.Image

        If e.Row.RowType = DataControlRowType.DataRow Then

            'Path aanvullen

            strFile = strFile & DataBinder.Eval(e.Row.DataItem, "Foto").ToString

            'Controleren of foto bestaat

            If File.Exists(strFile) Then

                'File exists => niets aanpassen

            Else

                'File doesn't exists => toon "unavailable" foto

                photo = CType(e.Row.FindControl("Foto"), System.Web.UI.WebControls.Image)

                photo.ImageUrl = "http://www.ryedb.net/files/images/unavailable.jpg"

            End If

        End If

    End Sub

Open in new window

0
 

Author Comment

by:DataconsultGent
ID: 21758146
Sorry I've forgot page load code
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Response.Cache.SetCacheability(HttpCacheability.NoCache)

    End Sub

Open in new window

0
 
LVL 12

Expert Comment

by:renjurdevan
ID: 21758173
If File.Exists(strFile) Then
                'File exists => niets aanpassen
            Else
                'File doesn't exists => toon "unavailable" foto
                photo = CType(e.Row.FindControl("Foto"), System.Web.UI.WebControls.Image)
                photo.ImageUrl = "http://www.ryedb.net/files/images/unavailable.jpg"
            End If


if file exists what will happen?? show pictures over there ... I hope strFile contains latest image path!!

 
0
 

Author Comment

by:DataconsultGent
ID: 21758265
If file exists then nothing should be changed and then is it the template field:
            <asp:TemplateField HeaderText="Picture">
                <ItemTemplate>
                    <asp:Image ID="Foto" runat="server" ImageUrl='<%# "http://www.ryedb.net/files/images/" & Eval("Foto") %>' />
                </ItemTemplate>
            </asp:TemplateField>

Eval("Foto") gives me the full url to the file (same as strFile but in stead of E:\websites ... it returns the "http path".

The file path is "District" & "_in_" & "id of the student" & "s" (small) + ".jpg" as it is saved with "strFullPathNewSmall" in the upload click button.

When a picture already exists and it is uploaded again, it overwrites the old one on the server, but when I display it it still gives me the old one, untill I hit F5 in my brower.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 12

Accepted Solution

by:
renjurdevan earned 500 total points
ID: 21758386
Try

    <asp:Image ID="Foto" runat="server" ImageUrl='<%# "http://www.ryedb.net/files/images/" & Eval("Foto") & "?id=" & DateTime.Now.MilleSecond.Tostring() %>' />
0
 
LVL 12

Expert Comment

by:renjurdevan
ID: 21758416
<asp:Image ID="Foto" runat="server" ImageUrl='<%# "http://www.ryedb.net/files/images/" & Eval("Foto") & "?id=" & DateTime.Now.Minute.ToString()&DateTime.Now.Millisecond.ToString()  %>' />

What I meant is that you should pass defferent values as query string eventhough we dont want any ! it avoid Caching  
0
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 21758435
yes renjurdevan has a point here.

Calling any file after passing some random value as a querystring parameter causes to load that file uniquely.

so u can say that
<asp:Image ID="Foto" runat="server" ImageUrl='<%# "http://www.ryedb.net/files/images/" & Eval("Foto") & "?id=" & Guid.NewGuid %>' />

i passed guid here because it is returns a unique number always.
0
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 21758445
ooh sorry renjurdevan, it really looks like that i copied your point but i was in progress of writing when u posted the second  comment.
0
 

Author Closing Comment

by:DataconsultGent
ID: 31466053
Thanks, great post.  Works perfect.
0
 

Author Comment

by:DataconsultGent
ID: 21758846
Thanks both for your answers.  I've tried it and it works.  I give the points to renjurdevan because his answer was the first one.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 53
In WPF / C# binding a local database in code behind 1 24
C# Single Form 8 29
Ajax Panel Not Updating When Triggered By Timer 4 46
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now