locked
Prevent loading big images .. RRS feed

  • Question

  • User1799966442 posted

    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


    Sunday, August 22, 2010 12:40 PM

Answers

  • User-1785506111 posted

    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.

    byte[] imageData = DownloadData([URI]);
    MemoryStream stream = new MemoryStream(imageData);
    Image img = Image.FromStream(stream);


    And the DownloadData function is:

            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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 22, 2010 1:25 PM

All replies

  • User-1852462877 posted

    Check the content length, etc, etc, etc...

                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;
                }

    See Ya!

    Monday, August 23, 2010 5:26 PM
  • User1799966442 posted

    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



    Sunday, August 29, 2010 2:47 PM
  • User-1785506111 posted

    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.

    byte[] imageData = DownloadData([URI]);
    MemoryStream stream = new MemoryStream(imageData);
    Image img = Image.FromStream(stream);


    And the DownloadData function is:

            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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 22, 2010 1:25 PM