[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.2

ASP.NET file upload locks file

Asked by dmat1 in Programming for ASP.NET

I have an ASP.Net file upload script that I use to upload and resize an image. It also creates a thumbnail. The problem is that if I try to upload a different image to replace the one I just uploaded I get an error. When I go to my FTP and try to delete it I get a permission denied. I can delete the file the next day though.

From what I can tell my script doesnt release the file immediately. Can someone take a look at my script and tell me what I am missing?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
<%@ Page Trace="False" Language="vb" aspcompat="false" debug="true" validateRequest="false"%>
<%@ Import Namespace=System.Drawing %>
<%@ Import Namespace=System.Drawing.Imaging %>
<%@ Import Namespace=System %>
<%@ Import Namespace=System.Web %>
<%@ Import Namespace="System.IO" %>
 
 
<SCRIPT LANGUAGE="VBScript" runat="server">
    Const Tx = 160 ' max width for thumbnails
    Const Ty = 120 ' max height for thumbnails
    Const Fx = 360' max width for full images
    Const Fy = 240' max height for full images
    Const upload_dir = "/img/agents/" ' directory to upload file
    Const upload_original = "big" ' filename to save original as (suffix added by script)
    Const upload_thumb = "small" ' filename to save thumbnail as (suffix added by script)
    Const upload_max_size = 5000 ' max size of the upload (KB) note: this doesn't override any server upload limits
    Dim fileExt ' used to store the file extension (saves finding it mulitple times)
    Dim newWidth, newHeight As Integer ' new width/height for the thumbnail
    Dim l2 ' temp variable used when calculating new size
    Dim fileFld As HttpPostedFile ' used to grab the file upload from the form
    Dim originalimg As System.Drawing.Image ' used to hold the original image
    Dim msg ' display results
    Dim upload_ok As Boolean ' did the upload work ?
    Dim MyFileName As String
    Dim CryptID  As String    
    
</script>
 
 
<%
 
 
    Randomize() ' used to help the cache-busting on the preview images
    upload_ok = False
    If LCase(Request.ServerVariables("REQUEST_METHOD")) = "post" Then
        fileFld = Request.Files(0) ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image)
        If fileFld.ContentLength > upload_max_size * 1024 Then
            msg = "Sorry, the image must be less than " & upload_max_size & "Kb"
        Else
            Try
                originalimg = System.Drawing.Image.FromStream(fileFld.InputStream)
                ' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height
                ' Note: if the original is smaller than the thumbnail size it will be scaled up
                If (originalimg.Width / Fx) > (originalimg.Width / Fy) Then
                    l2 = originalimg.Width
                    newWidth = Fx
                    newHeight = originalimg.Height * (Fx / l2)
                    If newHeight > Fy Then
                        newWidth = newWidth * (Fy / newHeight)
                        newHeight = Fy
                    End If
                Else
                    l2 = originalimg.Height
                    newHeight = Fy
                    newWidth = originalimg.Width * (Fy / l2)
                    If newWidth > Fx Then
                        newHeight = newHeight * (Fx / newWidth)
                        newWidth = Fx
                    End If
                End If
                Dim thumb As New Bitmap(newWidth, newHeight)
                'Create a graphics object
                Dim gr_dest As Graphics = Graphics.FromImage(thumb)
                ' just in case it's a transparent GIF force the bg to white
                Dim sb = New SolidBrush(System.Drawing.Color.White)
                gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height)
                'Re-draw the image to the specified height and width
                gr_dest.DrawImage(originalimg, 0, 0, thumb.Width, thumb.Height)
                Try
                 MyFileName = "agent" & request.querystring("pic_id")
 
                    fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower()
'                    originalimg.Save(Server.MapPath(upload_dir & MyFileName & "-" & upload_original & fileExt), originalimg.RawFormat)
                    thumb.Save(Server.MapPath(upload_dir & "/large/" & MyFileName & fileExt), originalimg.RawFormat)
 
 
 
        Dim bAns As Boolean
        Dim sNewFile As String
	Dim BMPFullPath as String
 
 
BMPFullPath=Server.MapPath(upload_dir & "/large/"  & MyFileName & fileExt)
 
 
        Try
            'bitmap class in system.drawing.imaging
            Dim objBmp As New Bitmap(BMPFullPath)
 
            'below 2 functions in system.io.path
            sNewFile =Server.MapPath(upload_dir) & "/large/" &  MyFileName & ".jpg"
            objBmp.Save(sNewFile, ImageFormat.Jpeg)
 
 
            bAns = True 'return true on success
        Catch
            bAns = False 'return false on error
        End Try
 
 
 
                    msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_dir & upload_original & fileExt)
                    upload_ok = True
		    
		    'Using the FileInfo class
		    
 
		    
                Catch ex As Exception
                    msg = "Sorry, there was a problem saving the image."
                End Try
                ' Housekeeping for the generated thumbnail
                If Not thumb Is Nothing Then
                    thumb.Dispose()
                    thumb = Nothing
                End If
            Catch
                msg = "Sorry, that was not an image we could process."
            End Try
       
 
 
 
'*******************
'large image
'*******************
 
Try
                originalimg = System.Drawing.Image.FromStream(fileFld.InputStream)
                ' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height
                ' Note: if the original is smaller than the thumbnail size it will be scaled up
                If (originalimg.Width / Tx) > (originalimg.Width / Ty) Then
                    l2 = originalimg.Width
                    newWidth = Tx
                    newHeight = originalimg.Height * (Tx / l2)
                    If newHeight > Ty Then
                        newWidth = newWidth * (Ty / newHeight)
                        newHeight = Ty
                    End If
                Else
                    l2 = originalimg.Height
                    newHeight = Ty
                    newWidth = originalimg.Width * (Ty / l2)
                    If newWidth > Tx Then
                        newHeight = newHeight * (Tx / newWidth)
                        newWidth = Tx
                    End If
                End If
                Dim full As New Bitmap(newWidth, newHeight)
                'Create a graphics object
                Dim gr_dest As Graphics = Graphics.FromImage(full)
                ' just in case it's a transparent GIF force the bg to white
                Dim sb = New SolidBrush(System.Drawing.Color.White)
                gr_dest.FillRectangle(sb, 0, 0, full.Width, full.Height)
                'Re-draw the image to the specified height and width
                gr_dest.DrawImage(originalimg, 0, 0, full.Width, full.Height)
                Try
 
	    
                    fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower()
'                    originalimg.Save(Server.MapPath(upload_dir & MyFileName & "-" & upload_original & fileExt), originalimg.RawFormat)
                    full.Save(Server.MapPath(upload_dir & "/small/" & MyFileName & fileExt), originalimg.RawFormat)
 
 
 
        Dim bAns As Boolean
        Dim sNewFile As String
	Dim BMPFullPath as String
 
 
BMPFullPath=Server.MapPath(upload_dir & "/small/"  & MyFileName & fileExt)
 
 
        Try
            'bitmap class in system.drawing.imaging
            Dim objBmp As New Bitmap(BMPFullPath)
 
            'below 2 functions in system.io.path
            sNewFile =Server.MapPath(upload_dir) & "/small/" &  MyFileName & ".jpg"
            objBmp.Save(sNewFile, ImageFormat.Jpeg)
objBmp.Dispose()
            bAns = True 'return true on success
        Catch
            bAns = False 'return false on error
        End Try
 
 
 
                    msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_dir & upload_original & fileExt)
                    upload_ok = True
		    
		    'Using the FileInfo class
		    
 
		    
                Catch ex As Exception
                    msg = "Sorry, there was a problem saving the image."
                End Try
                ' Housekeeping for the generated thumbnail
                If Not full Is Nothing Then
                    full.Dispose()
                    full = Nothing
                End If
            Catch
                msg = "Sorry, that was not an image we could process."
            End Try
        End If
 
 
 
 
 
 
 
 
        ' House Keeping !
        If Not originalimg Is Nothing Then
 
            originalimg.Dispose()
            originalimg = Nothing
        End If
    End If
%>
<html>
<head>
<LINK href="/rhino.css" type=text/css rel=stylesheet>
</head>
<body>
<center>
 
<table width=550 bgcolor=white><tr><td align=center><br>
<br>
 
 
 
<p>
    &nbsp;</p>
 
 <form id="Form1" enctype="multipart/form-data" method="post" runat="server">
 <table>
 <tr><td>Select the new file to upload:</td></tr><tr><td><input type="file" name="upload_file"></td></tr>
 
 
 <tr><td colspan=1><input type="submit" value="Upload" id="Submit1" language="javascript" onclick="return Submit1_onclick()">
 </table>
 </form>
 
 <%
     If upload_ok Then
 
%>
<script language="JavaScript" type="text/javascript">
opener.agentpic.src="<%=upload_dir & "/large/" & MyFilename   &  ".jpg?" & rnd()%>";
window.close();
</script>
 
 
 <table>
 <tr>
 
 <td valign=top align=center>
 
<img src="<%=upload_dir & "/large/" & MyFilename   &  ".jpg?" & rnd()%>"></td>
 </tr>
 </table>
 <%
 Else
     Response.Write(msg)
 End If
 %><br>
 
</td></tr></table>
 
</center>
</body>
</html>
[+][-]01/24/09 05:30 AM, ID: 23456287Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/24/09 05:31 AM, ID: 23456296Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/18/09 03:01 AM, ID: 23668906Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/18/09 03:38 PM, ID: 23676437Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/21/09 04:50 PM, ID: 23702736Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/23/09 02:38 AM, ID: 23709767Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/17/09 11:54 AM, ID: 23911221Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Programming for ASP.NET
Sign Up Now!
Solution Provided By: suchandsuch
Participating Experts: 3
Solution Grade: A
 
[+][-]06/18/09 09:41 AM, ID: 24659179Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625