I've just made the change from ASP to .net.
I have a file (code below) that saves a user submitted file to a MS SQL 2005 database. It collects the file name, file size, file type, and lastly the binary data for the file.
I can sucessfully take the files out of the databse again and display them in a data grid.
I would like to resize the submitted file to a fixed size (say 180 x 120) before I upload it to the database and do this without saving it to a temporary file on the servers HDD.
I also do not want to use the "thumbnail method" as I want to keep the quality of the resized smaller images as high as possible.
The code below is stripped down and does not have any validation to check for files that are not images etc, I will add that later. I also don't mind if i can only rezise and store in the database jpeg and gif files, but I need to be able handle at least these two image types.
Expand|Select|Wrap|Line Numbers
- <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1"%>
- <%@ Import Namespace="System.IO" %>
- <%@ Import Namespace="System.Data" %>
- <%@ Import Namespace="System.Data.OleDb" %>
- <%@ Import Namespace="System.Configuration" %>
- <script language="VB" runat="server">
- Public Sub UploadBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
- 'Get the posted file
- Dim fileDataStream As Stream = MyFile.PostedFile.InputStream
- 'get file type
- Dim FileType As string = MyFile.PostedFile.ContentType
- 'Get size of file
- Dim fileLength As Integer = MyFile.PostedFile.ContentLength
- 'Create a byte array with file length
- Dim fileData() As Byte = New Byte(fileLength) {}
- 'Read the stream into the byte array
- fileDataStream.Read(fileData,0,fileLength)
- 'get file name (excluding the path)
- Dim strFileNamePath as String = MyFile.PostedFile.FileName
- Dim intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
- Dim strFileNameOnly as String = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
- Dim DBConnection As New OleDbConnection(ConfigurationSettings.AppSettings("MSEMM_Connection_string"))
- Dim UpdateCommand as new oledbcommand
- UpdateCommand.Connection = DBConnection
- UpdateCommand.Commandtype = Commandtype.text
- UpdateCommand.Commandtext= "INSERT INTO dbo.Files (FileName, FileSize, ContentType, FileData) Values(?,?,?,?)"
- UpdateCommand.Parameters.AddWithValue("?", strFileNameOnly)
- UpdateCommand.Parameters.AddWithValue("?", fileLength)
- UpdateCommand.Parameters.AddWithValue("?", FileType)
- UpdateCommand.Parameters.AddWithValue("?", fileData)
- DBConnection.open()
- UpdateCommand.ExecuteNonQuery
- DBConnection.Close()
- End Sub
- </script>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
- <title>file upload test</title>
- </head>
- <body>
- <h3>File Upload</h3>
- <hr>
- <asp:label id="Message" Text="Select a file and filename" runat="server"/>
- <hr>
- <h3>File Upload</h3>
- <form enctype="multipart/form-data" runat="server">
- File: <input id="myFile" type="file" runat="server">
- <input type=button value="Upload" OnServerClick="UploadBtn_Click" runat="server">
- </form>
- </body>
- </html>