ASP.NET: Uploading an Image and creating a thumbnail Support Product Page
This topic was archived
No Upload, No Action at All
Reported 23 Jan 2005 09:59:20
1
has this problem
23 Jan 2005 09:59:20 tom fox posted:
I have completed the tutorial but nothing is uploading, in fact nothing is happening at all. I have pasted my code below. In the future I would propose icluding a working sample for two reasons, one the author will have had to actually run the code and see it work, two we have a full page of code to look at to see if we misread a direction or something. Anyway here is my code:<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3.org/TR/html4/loose.dtd">
<script runat="server">
Sub btnSubmit_Click(sender As Object, e As System.EventArgs)
If Not File1.PostedFile Is Nothing Then
SaveImage()
End If
End Sub
Sub SaveImage()
Dim F As New System.IO.FileInfo(File1.PostedFile.FileName)
If F.Extension.ToLower() = ".gif" Or F.Extension.ToLower() = ".jpg" Then
File1.PostedFile.SaveAs(Request.PhysicalApplicationPath & " \cart\images\productImages\ " + F.Name)
MakeThumbNail(F, 150, 80)
Else
Response.Write("wrong file type"
End If
End Sub
Sub MakeThumbNail(F As System.IO.FileInfo, MaxWidth As Double, MaxHeight As Double)
Dim OriginalImg As System.Drawing.Image = System.Drawing.Image.FromStream(File1.PostedFile.InputStream)
Dim TheSize As new System.Drawing.Size(OriginalImg.Width, OriginalImg.Height)
Dim sizer As Double = 1
Do While(MaxWidth > -1 And TheSize.Width > MaxWidth) Or (MaxHeight > -1 And TheSize.Height > MaxHeight)
If MaxWidth > -1 And TheSize.Width > MaxWidth Then
sizer = MaxWidth / TheSize.Width
TheSize.Width = Convert.ToInt32(TheSize.Width * sizer)
TheSize.Height = Convert.ToInt32(TheSize.Height * sizer)
End If
If MaxHeight > -1 And TheSize.Height > MaxHeight Then
sizer = MaxHeight / TheSize.Height
TheSize.Width = Convert.ToInt32(TheSize.Width * sizer)
TheSize.Height = Convert.ToInt32(TheSize.Height * sizer)
End If
Loop
Dim SavePath As String = Request.PhysicalApplicationPath & " \cart\images\productImages\thumbs\ " & F.Name
Dim NewImg As New System.Drawing.Bitmap(OriginalImg, TheSize)
OriginalImg.Dispose()
If System.IO.File.Exists(SavePath) Then System.IO.File.Delete(SavePath)
Select Case F.Extension.ToLower()
Case ".jpg"
NewImg.Save(SavePath, System.Drawing.Imaging.ImageFormat.Jpeg)
Case ".gif"
NewImg.Save(SavePath, System.Drawing.Imaging.ImageFormat.Gif)
End Select
NewImg.Dispose()
End Sub
</script>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" enctype="multipart/form-data" id="form1" runat="server" action="">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>File</td>
<td> </td>
</tr>
<tr>
<td><input type="file" name="File1" id="File1" runat="server"></td>
<td> </td>
</tr>
<tr>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>