Solved

Image refresh file IO problem or cache or something else?

Posted on 2008-06-11
11
351 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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