System.IO.FileNotFoundException: Could not find file

Discussion in 'ASP.NET 2.0' started by Tina Wilson, Feb 25, 2008.

  1. I am uploading an image to my database using the file upload control. I also have a method that is creating a thumbnail and saving THAT to the database from the original image.

    The error I get looks like this:

    Exception Details: System.IO.FileNotFoundException: Could not find file
    'c:\windows\system32\inetsrv\Sunset.jpg'.

    Even though the original image is here:

    C:\Documents and Settings\All Users\Documents\My Pictures\Sample
    Pictures

    My question is where is it getting that first path from and why would it matter if the file was found or not since I'm not saving anything to disk....it'sall being saved to the database.

    HELP!

    Here is my method that is creating the thumbnail:




    public byte[] GetThumbnailImage(string fileName)


    {


    int thumbnailHeight;


    int thumbnailWidth;


    int maxThumbSize = 150;


    double hx; double wx;


    System.Drawing.Image myimage;


    myimage = System.Drawing.Image.FromFile(fileName);


    SessionState.ImageHeight = myimage.Height;


    SessionState.ImageWidth = myimage.Width;


    hx = myimage.Height;


    wx = myimage.Width;


    // calc the thumbnail Width and Height: -scale to max with


    if (myimage.Width > myimage.Height) // landscape: width is fixed,scale height:


    {


    thumbnailWidth = maxThumbSize;


    thumbnailHeight = Convert.ToInt32((hx / wx) * thumbnailWidth);


    }


    else // portrait: height is fixed at max, scale width:


    {


    thumbnailHeight = maxThumbSize;


    thumbnailWidth = Convert.ToInt32((wx / hx) * thumbnailHeight);


    }


    System.Drawing.Image thumbnailImage = myimage.GetThumbnailImage(thumbnailWidth, thumbnailHeight, null, IntPtr.Zero);


    MemoryStream ms = new MemoryStream();


    string imageFormat = Path.GetExtension(ImageUpload.PostedFile.FileName).Replace(".", "").ToUpper();


    thumbnailImage.Save(ms,this.GetImageFormat(imageFormat) );


    return ms.ToArray();


    }





    Here is the code that is part of the save button that saves the original image to the DB:





    Photo photo = new Photo();


    photo.CategoryId = Convert.ToInt32(this.CategoryList.SelectedValue);


    photo.PhotoName = this.PhotoName.Text;


    photo.UserId = this.UserId;


    byte[] image;


    string fileName = ImageUpload.PostedFile.FileName;





    //// Open File and Read Into Byte Array


    using (FileStream fs = new FileStream(fileName, FileMode.Open))


    {


    BinaryReader reader = new BinaryReader(fs);


    image = reader.ReadBytes((int)fs.Length);


    fs.Close();


    }





    photo.ImageFormat = Path.GetExtension(ImageUpload.PostedFile.FileName).Replace(".", "").ToUpper();


    photo.ContentType = ImageUpload.PostedFile.ContentType;


    photo.PhotoMember = image;


    photo.Thumbnail = this.GetThumbnailImage(fileName);


    photo.Caption = this.Caption.Text;


    photo.DateCreated = DateTime.Now;


    photo.ContentLength = ImageUpload.PostedFile.ContentLength;


    photo.ImageHeight = this.ImageHeight;


    photo.ImageWidth = this.ImageWidth;


    photo.Save();
     
  2. You have a Memory Stream and a File Stream but no paths or connections.
    btw, do you really need to dump files into the Database?
    All the best,
    Mark
     
  3. What do you mean paths or connections? The file stream has the path of the original file it is uploading and I pass that same path into the method that is creating the thumbnail.



    string fileName = ImageUpload.PostedFile.FileName;





    //// Open File and Read Into Byte Array


    using (FileStream fs = new FileStream(fileName, FileMode.Open))


    {


    BinaryReader reader = new BinaryReader(fs);


    image = reader.ReadBytes((int)fs.Length);


    fs.Close();


    }


    ...and if you mean connection to the database that is handled somewhere else.This works on my local machine BTW, just not in my deployed version on discountasp.net.


    The database part was not my choice! :0( It was required by management.
     

Share This Page