upload og thumbnail
Først: jeg er ny i asp.netJeg har fundet denne kode til at uploade og generere en thumbnail.
http://www.eksperten.dk/spm/259027
Men jeg får fejlen:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: C:\Documents and Settings\Ernesto Mastroianni\Dokumenter\Billeder\modeller\eva\DSCN3059.JPG
Source Error:
Line 17: Dim strFileName As String = Path.GetFileName(strFileNamePath) 'Gets the filename
Line 18:
Line 19: aImgOrginal = aImgOrginal.FromFile(strFileNamePath)
Line 20:
Line 21: Dim ImgStream As Stream = ImageFile.PostedFile.InputStream
Koden:
<%@ Page Language="vb" debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace=System.Drawing %>
<script language="vb" runat="server">
Sub UploadImage(sender as object, e as EventArgs)
'-----------------------------------------------------------------------------------------------------
'VARIABLES
'-----------------------------------------------------------------------------------------------------
Dim aImgOrginal, aImgThumb As System.Drawing.Image
Dim inp As New IntPtr()
Dim aIntWidth, aIntHeight As Integer
Dim aIntTempMultiplier As Integer
Dim strFileNamePath as string = ImageFile.PostedFile.FileName 'Gets the filename and Path
Dim strFileName As String = Path.GetFileName(strFileNamePath) 'Gets the filename
aImgOrginal = aImgOrginal.FromFile(strFileNamePath)
Dim ImgStream As Stream = ImageFile.PostedFile.InputStream
Dim OriginalImage(ImageFile.PostedFile.ContentLength) As Byte
ImgStream.Read(OriginalImage, 0, ImageFile.PostedFile.ContentLength)
'-----------------------------------------------------------------------------------------------------
'DEFINE THUMBNAIL SIZE, BASED ON ORIGINAL IMAGE SIZE
'-----------------------------------------------------------------------------------------------------
If aImgOrginal.Height > aImgOrginal.Width then ' portrait
aIntTempMultiplier = 100 / aImgOrginal.Height
Else 'landscape
aIntTempMultiplier = 100 / aImgOrginal.Width
End if
aIntWidth = aImgOrginal.Width * aIntTempMultiplier
aIntHeight = aImgOrginal.Height * aIntTempMultiplier
'-----------------------------------------------------------------------------------------------------
'SAVE THUMBNAIL TO DISK
'-----------------------------------------------------------------------------------------------------
aImgThumb = aImgOrginal.GetThumbnailImage(aIntWidth, aIntHeight, Nothing, inp)
aImgThumb.Save(Server.Mappath(strFileName), Imaging.ImageFormat.Jpeg)
'-----------------------------------------------------------------------------------------------------
'CREATE A FILESTREAM, SO WE CAN CONVERT THE THUMB TO BYTE
'-----------------------------------------------------------------------------------------------------
Dim fs as FileStream = New FileStream(Server.Mappath(strFileName),FileMode.OpenOrCreate,FileAccess.Read)
Dim ThumbNail(fs.Length) As Byte
fs.Read(ThumbNail, 0, Convert.ToInt32(fs.Length))
fs.Close()
'-----------------------------------------------------------------------------------------------------
'INSERT INFO INTO YOUR DATABASE
'-----------------------------------------------------------------------------------------------------
Dim objConn as New SQLClient.SQLConnection("server=localhost;uid=sa;pwd=;Database=BetFiguerasCOM")
Dim cmdText = "INSERT INTO pclPROJECTS_PICTURES(Picture,ThumbNail) VALUES(@image,@ThumbNail)"
Dim objcmd As New SqlClient.SqlCommand(cmdText, objConn)
Dim imgparam As New SqlClient.SqlParameter("@image", SqlDbType.Image)
Dim imgthumb As New SqlClient.SqlParameter("@ThumbNail", SqlDbType.Image)
imgparam.Value = OriginalImage
imgthumb.Value = ThumbNail
objcmd.Parameters.Add(imgparam)
objcmd.Parameters.Add(imgthumb)
objconn.Open()
objcmd.ExecuteNonQuery()
objconn.close()
'-----------------------------------------------------------------------------------------------------
'Now we are finished, delete the temporary thumbnail
'-----------------------------------------------------------------------------------------------------
If (File.Exists(Server.Mappath(strFileName))) Then
File.Delete(Server.Mappath(strFileName))
End If
End Sub
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<form enctype="multipart/form-data" runat="server" id="frmUpload">
<input id="ImageFile" runat="server" type="file" /><p>
<asp:button runat="server" OnClick="UploadImage" text="Upload pictures"/>
</form>
</body>
</html>
