HttpPostedFile myFile = filUpload.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
{
lblOutput.Text = "No file was uploaded.";
return;
}
// Check file extension (must be JPG)
if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
{
lblOutput.Text = "The file must have an extension of JPG";
return;
}
I had a project where I had to grab images from across the web. Below is the code I use to get that data and load it as an Image object. You can then use that object to check the image's dimensions and such.
private byte[] DownloadData(string uri)
{
byte[] data = new byte[0];
try
{
WebRequest req = WebRequest.Create(uri);
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
//Download in chuncks
byte[] buffer = new byte[1024];
//Get Total Size
int dataLength = (int)response.ContentLength;
//Download to memory
//Note: adjust the streams here to download directly to the hard drive
MemoryStream memStream = new MemoryStream();
while (true)
{
//Try to read the data
int bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
//Write the downloaded data
memStream.Write(buffer, 0, bytesRead);
}
data = memStream.ToArray();
stream.Close();
memStream.Close();
}
catch (Exception)
{
MessageBox.Show("There was a problem downloading the file");
}
return data;
}
You could get the size of an image using the ContentLength property of the response object in the DownloadData function.
Member
22 Points
9 Posts
Prevent loading big images ..
Aug 22, 2010 12:40 PM|LiviusPL|LINK
Hi
i need to prevent loading big images e.g max 100KB
but i only store url
1. user put url with image (i remember this string in database not image itself)
this cause that i can not check image size before store url in database
because user can store url and after this operation put bigger file at that location)
2. my web page generate some different image based on first image
and i need to load portion of image
to stream and set buffer size to 100KB + 1 Byte
if stream is larger than 100KB that user put bigger file
how can i accomplish that?
FileStream can not use URI as location only disc location
what should i use instead?
please help,
Karol Bieniaszewski
Participant
852 Points
264 Posts
Re: Prevent loading big images ..
Aug 23, 2010 05:26 PM|CandorZ|LINK
Check the content length, etc, etc, etc...
See Ya!
Member
22 Points
9 Posts
Re: Prevent loading big images ..
Aug 29, 2010 02:47 PM|LiviusPL|LINK
This will only work if user send me a file from hard disk.
but in my situation user post me only web url not file from disk
I need to load some portion of data(portion of this file) in packets - and stop loading if i reach my limit in KB
please help
Member
10 Points
4 Posts
Re: Prevent loading big images ..
Sep 22, 2010 01:25 PM|hoagsie|LINK
I had a project where I had to grab images from across the web. Below is the code I use to get that data and load it as an Image object. You can then use that object to check the image's dimensions and such.
And the DownloadData function is:
You could get the size of an image using the ContentLength property of the response object in the DownloadData function.