|
there is a file size restriction in the web.config file, i don't remember what it is exactly but it has a default of 4 mb.
|
|
|
|
|
Add httpRuntime maxRequestLength="24576" to the system.web section.
24576 will allow 24 MB uploads, you can adjust this as needed.
Correct, the default is 4 MB.
Hope this helps.
Victor
-- modified at 13:19 Monday 24th April, 2006
|
|
|
|
|
i want to upload all files from selecting the folder or specify that folder path.
one by one file will be upload on web server path using .NET application as well as vb6 application.
sachin
|
|
|
|
|
I have the following error,,
would u please help me for it,
Access to the path "c:\inetpub\wwwroot\testimgupload\images\ppp.jpg" is denied
and it point to the following line:
Dim newFile As System.IO.FileStream = New System.IO.FileStream(Server.MapPath(sSavePath + sFilename), System.IO.FileMode.Create)
|
|
|
|
|
Check the permissions on that folder, using windows explorer right click and select properties, then select security (for xp pro or sharing xp home) then check that iusr has write permissions on that folder.
|
|
|
|
|
System.Drawing.Image.GetThumbnailImageAbort myCallBack =
Line 61: new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
Line 62: Bitmap myBitmap;
Line 63: try
Line 64: {
Compiler Error Message: CS0246: The type or namespace name 'Bitmap' could not be found (are you missing a using directive or an assembly reference?)
izzit i need add in somthing in the webconfig?,, i do this code in webmatrix,, any solution?
|
|
|
|
|
Make sure you have a reference to the System.Drawing namespace at the top of your code file (e.g. "using System.Drawing;")
|
|
|
|
|
When i ran the example, upon clicking upload button, this message appear:
Server Error in '/Example' Application.
--------------------------------------------------------------------------------
Could not find a part of the path "c:\inetpub\wwwroot\Example\images\che_guevara.jpg".
Source Error:
Line 96:
Line 97: // Save the stream to disk
Line 98: System.IO.FileStream newFile = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename), System.IO.FileMode.Create);
Line 99: newFile.Write(myData,0, myData.Length);
Line 100: newFile.Close();
Error on line 98.
Hope you can help,TQ!
|
|
|
|
|
I would like to upload a file in to sql database. Anyone has it in .NET syntax?
For the method you just did. when the upload button is clicked, it is uploaded to a folder in my local computer. If someone else using a different computer uploads pic. Would it be to my computer?
Lastly.....just to know....can i use type C# code on a webform? would it work?
I am actually new in programming and is trying to create a website like ebay.
just for fun! Am stuck...help me pls someone =)
|
|
|
|
|
|
This tool is quite cool. But don't you think that the size of the thumbnail should be dependant on the original picture file? Well I do... I get the original picture size, calculate the width/height ratio, initialize only one dimension of the thumbnail size and then... calculate the rest. (floatSizeRatio and intBiggerDim being newly added variables)
// calculate thumbnail size
floatSizeRatio = myBitmap.PhysicalDimension.Height/myBitmap.PhysicalDimension.Width;
if (floatSizeRatio > 1)
{
// portrait image
intThumbHeight = intBiggerDim;
intThumbWidth = (int) (intThumbHeight / floatSizeRatio);
}
else
{
// landscape image
intThumbWidth = intBiggerDim;
intThumbHeight = (int) (intThumbWidth * floatSizeRatio);
}
|
|
|
|
|
I agree that this would be essential. However, if you were to do this the imgPicture should also have its width and Height set in a similar manner.
When I try this, I get the following error:
Value of type 'Integer' cannot be converted to 'System.Web.UI.WebControls.Unit'
Any ideas?
|
|
|
|
|
It seems that when setting the image hight or width, that a Unit is required.
' Calculate the size ratio
dblfloatSizeRatio = mybitmap.PhysicalDimension.Height / mybitmap.PhysicalDimension.Width
If dblfloatSizeRatio > 1 Then
intThumbHeight = intBiggerDim
intThumbWidth = (intThumbHeight / dblfloatSizeRatio)
Else
intThumbWidth = intBiggerDim
intThumbHeight = (intThumbWidth * dblfloatSizeRatio)
End If
Dim mythumbnail As System.Drawing.Image
mythumbnail = mybitmap.GetThumbnailImage(intThumbWidth, intThumbHeight, myCallBack, IntPtr.Zero)
mythumbnail.Save(Server.MapPath(sSavePath + sThumbFile))
ImgPicture.ImageUrl = sSavePath + sThumbFile
' Set the imgPicture height and width
imgPicture.Height = New Unit(intThumbHeight)
imgPicture.Width = New Unit(intThumbWidth)
|
|
|
|
|
do you have to explicitly call the dispose method on the image?
|
|
|
|
|
Is there a way to limit the JPG file size to 100kb?
Thanks.
|
|
|
|
|
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
'Initialize variables
Dim sSavePath As String
Dim sThumbExtension As String
Dim intThumbWidth As Integer
Dim intThumbHeight As Integer
'Set constant values
sSavePath = "~/images/"
sThumbExtension = "_thumb"
intThumbWidth = 160
intThumbHeight = 120
'If file field isn’t empty
If IsNothing(filUpload.PostedFile) = False Then
'Check file size (mustn’t be 0)
Dim myFile As HttpPostedFile = filUpload.PostedFile
Dim nFileLen As Integer = myFile.ContentLength
If nFileLen = 0 Then
lbloutput.Text = "No file was uploaded."
Return
End If
'Check file extension (must be JPG)
If System.IO.Path.GetExtension(myFile.FileName).ToLower() <> ".jpg" Then
lbloutput.Text = "The file must have an extension of JPG"
Return
End If
'Read file into a data stream
Dim myData() As Byte = New Byte(nFileLen) {}
myFile.InputStream.Read(myData, 0, nFileLen)
'Make sure a duplicate file doesn’t exist. If it does, keep on appending an
'incremental numeric until it is unique
Dim sFilename As String = System.IO.Path.GetFileName(myFile.FileName)
Dim file_append As Integer = 0
While System.IO.File.Exists(Server.MapPath(sSavePath + sFilename))
file_append = file_append + 1
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) & file_append.ToString() + ".jpg"
End While
'Save the stream to disk
Dim newFile As System.IO.FileStream = New System.IO.FileStream(Server.MapPath(sSavePath + sFilename), System.IO.FileMode.Create)
newFile.Write(myData, 0, myData.Length)
newFile.Close()
' Check whether the file is really a JPEG by opening it
Try
' If jpg file is a jpeg, create a thumbnail filename that is unique.
file_append = 0
Dim sThumbFile As String = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) & sThumbExtension + ".jpg"
While System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile))
file_append = file_append + 1
sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) & file_append.ToString(+sThumbExtension + ".jpg")
End While
Dim ThumbnailCallback As System.Drawing.Image.GetThumbnailImageAbort
Dim myCallBack As System.Drawing.Image.GetThumbnailImageAbort
' Save thumbnail and output it onto the webpage
Dim mybitmap As Bitmap
mybitmap = New Bitmap(Server.MapPath(sSavePath + sFilename))
Dim mythumbnail As System.Drawing.Image
mythumbnail = mybitmap.GetThumbnailImage(intThumbWidth, intThumbHeight, myCallBack, IntPtr.Zero)
mythumbnail.Save(Server.MapPath(sSavePath + sThumbFile))
Imgpicture.ImageUrl = sSavePath + sThumbFile
' Displaying success information
lbloutput.Text = "File uploaded successfully!"
' Destroy objects
mythumbnail.Dispose()
mybitmap.Dispose()
Catch errArgument As ArgumentException
' The file wasn't a valid jpg file
lbloutput.Text = "The file wasn't a valid jpg file."
System.IO.File.Delete(Server.MapPath(sSavePath + sFilename))
End Try
End If
End Sub
|
|
|
|
|
Remeber to change the line containing the following:
Dim sThumbFile As String = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) & sThumbExtension + ".jpg"
to
Dim sThumbFile As String = System.IO.Path.GetFileNameWithoutExtension(sFilename) & sThumbExtension + ".jpg"
Otherwise it will fail if the thumbnail already exists
|
|
|
|
|
THANK YOU SO MUCH! I've been looking for working VB code of "uploading file" for a some time now. I almost gave up until saw this and thought "well ok, let's give this one a shot". And it was worth it. Finally can proceed with my work. Thanks again!
|
|
|
|
|
Thanks a million for this - been searching high and low for VB.Net code.
|
|
|
|
|
'Initialize variables
Dim sSavePath As String
Dim sThumbExtension As String
Dim intThumbWidth As Integer
Dim intThumbHeight As Integer
'Set constant values
sSavePath = "images/" & ddlSaveFolder.SelectedItem.Text & "/"
sThumbExtension = "_thumb"
intThumbWidth = 160
intThumbHeight = 120
'If file field isn’t empty
If IsNothing(filUpload.PostedFile) = False Then
'Check file size (mustn’t be 0)
Dim myFile As HttpPostedFile = filUpload.PostedFile
Dim nFileLen As Integer = myFile.ContentLength
If nFileLen = 0 Then
lblOutput.Text = "No file was uploaded."
Return
End If
'Check file extension (must be JPG)
If System.IO.Path.GetExtension(myFile.FileName).ToLower() <> ".jpg" Then
lblOutput.Text = "The file must have an extension of JPG"
Return
End If
'Read file into a data stream
Dim myData() As Byte = New Byte(nFileLen) {}
myFile.InputStream.Read(myData, 0, nFileLen)
'Make sure a duplicate file doesn’t exist. If it does, keep on appending an
'incremental numeric until it is unique
Dim sFilename As String = System.IO.Path.GetFileName(myFile.FileName)
Dim file_append As Integer = 0
While System.IO.File.Exists(Server.MapPath(sSavePath + sFilename))
file_append = file_append + 1
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) & file_append.ToString() + ".jpg"
End While
If HttpContext.Current.User.Identity.IsAuthenticated Then
'Save the stream to disk
Dim newFile As System.IO.FileStream = New System.IO.FileStream(Server.MapPath(sSavePath + sFilename), System.IO.FileMode.Create)
newFile.Write(myData, 0, myData.Length)
newFile.Close()
End If
|
|
|
|
|
Hi again
Why is the thumbnail saved as an .PNG file, while the bigger version of the file is an .JPG file?
|
|
|
|
|
Hi!
Why do I get the error: "File not a valid jpg file"?
When I run the script both files are save at the server, but I can't open the _thumb file neither in Photoshop or any other program.
Hope someone can help because it's a very cool script and I would really like it to work.
Larz
|
|
|
|
|
I had the same problem. It seems you need to tell .net the type of image file it is when you're saving the thumbnail. See code below:
//Put these classes at the tip of your class file
//I don't like writing these out all throughout the code
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
//Put this code at the top of the button click event
//initialize the type variable
ImageFormat imgFrmt = null;
//I replaced his check for a valid jpg with a check for gif, jpg or png
if (imgFile.ContentType != "image/pjpeg" && imgFile.ContentType != "image/gif" && imgFile.ContentType != "image/x-png")
{
lblOutput.Text = "The file must have an extension of JPG,GIF or PNG";
return;
}
//Put the code below in just before you try to save myThumbnail
//I added more code to his example to handle more image types so I have a
//switch to determine which format this file is in
//If you're only using one type, you could just initialize the variable
//above something like (ImageFormat imgFrmt = ImageFormat.Jpeg;)
switch(Convert.ToString(Path.GetExtension(imgFile.FileName)).ToLower())
{
case ".gif":
imgFrmt = ImageFormat.Gif;
break;
case ".jpeg":
case ".jpg":
imgFrmt = ImageFormat.Jpeg;
break;
case ".png":
imgFrmt = ImageFormat.Png;
break;
} //end switch
//Now when you use his code to save the image
//add the format parameter "imgFrmt" to it:
myThumbnail.Save (Server.MapPath(sSavePath + sThumbFile), imgFrmt);
Charles T.
|
|
|
|
|
Not sure if this is a useful shortcut, or if I'm going to cause myself a lot of hassle later, but I did the following:-
First, I checked the mime-type to ensure the file was a JPG/GIF/PNG - I figure this is more fool-proof than just checking the extension.
Then, instead of saving the file and then opening the file up into the bitmap, I read the InputStream directly into a new Bitmap:-
Bitmap objBitmap = new Bitmap(myFile.InputStream);
From here, I can check the dimensions of the image, determine if I need to create a thumbnail (or if the image is small enough to act as its own), etc. You can use Bitmap.Save(..) to store the image - if storing to a DB Blob, I'm guessing you could either Read the Stream first into a Byte[] array, or write the Bitmap into the Blob.
So far, I haven't had any problems with this, but if there is something I've missed I'm sure one of you will know.
Two areas I'll like to sort out:-
A) Is there a better way to generate a unique filename? (they don't have to be user-friendly, since the user-friendly name is stored in the database, with its associated unique filename)
B) when sending the image back to the browser (I'm using a GetImage.aspx?user=2&img=3 setup), can I attach the user-friendly name to the image - for example if they right-click and Save Picture As... the filename in the 'Save As' dialog would be the user-friendly name stored in the database.
Yours,
Ann-Marie Ratcliffe
|
|
|
|
|
Is there any article on retrieving files from database where user had uploaded earlier?
|
|
|
|