The process cannot access...IIS7, same code works in IIS5

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

The process cannot access...IIS7, same code works in IIS5

Guest-976
I get this strange message when uploading a file with NeatUpload activated.

"The process cannot access the file 'C:\Windows\TEMP\b467af777adc4cb8864c569d9a47bdda.config' because it is being used by another process."


The code works great on IIS 5 and 6 but with IIS7 i keep getting this message.

As you could see in the code i get the filename and extension, that works great! But when getting fileContent.Length the error message pops up!

Any hints what can be wrong?

Thanks
// David

The code:
    protected void Page_Load(object sender, EventArgs e)
    {
   
        int imageHeight;
        if(!int.TryParse(Request.QueryString["h"], out imageHeight)) imageHeight = 0;
       
        int imageWidth;
        if(!int.TryParse(Request.QueryString["w"], out imageWidth)) imageWidth = 0;
       
        if(!int.TryParse(Request.QueryString["siteid"], out siteid)) siteid = -1;
       
    }
       
    private void DoUpload(object sender, EventArgs e)
    {
        int maxWidth = 1024;
        int maxHeight = 1024;
       
        string sPath;
        string imagePath;
        string sFile;
        string sSplit;

       
        SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connStringSql"].ToString());
       
        sFile = txtUpload.FileName;

        sSplit = sFile.Split(new char[] { '\n' })[0];

        fileext = sSplit.Substring(sSplit.LastIndexOf('.') + 1, sSplit.Length - (sSplit.LastIndexOf('.') + 1));
       
       
       
        string sSQL = "INSERT INTO tblImage (pageID, siteID, strFilename, intFileSize, strFileExt) VALUES (-1," + siteid + ",'" + sFile + "'," + txtUpload.FileContent.Length + ",'" + fileext + "'); select @@identity;";
       
        SqlCommand cmd = new SqlCommand(sSQL,conn);
       
        using(conn)
        {
            conn.Open();
            int.TryParse(cmd.ExecuteScalar().ToString(), out newid);
       
        }
        ImageFormat imageformat = ImageFormat.Jpeg;
        System.Drawing.Image uploadImage;
       
        try
        {
            uploadImage = System.Drawing.Image.FromStream(txtUpload.FileContent);
            imagePath = @"/images/imagedb/" + siteid + @"/";
            sPath = Server.MapPath(imagePath);
           
           
                   
            switch(fileext)
            {
                case "png":
                {
                    imageformat = ImageFormat.Png;
                    break;
                }
                case "gif":
                {
                    imageformat = ImageFormat.Gif;
                    break;
                }
                default:
                {
                    imageformat = ImageFormat.Jpeg;
                   
                break;
                }
            }

            ImageCodecInfo theEncoder = GetEncoder(imageformat);

            System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
            EncoderParameters myEncoderParameters = new EncoderParameters(1);

            myEncoderParameters.Param[0] = new EncoderParameter(myEncoder, Convert.ToInt64(100));
           
            if(uploadImage.Width > maxWidth || uploadImage.Height > maxHeight)
            {
                int widthOrig, heightOrig, widthTh, heightTh;
                decimal fx, fy, f;
               
                widthOrig = uploadImage.Width;
                heightOrig = uploadImage.Height;
               
                fx = widthOrig/maxWidth;
                fy = heightOrig/maxHeight;
               
                f= Math.Max(fx,fy);
               
                if(f<1)
                {
                    f=1;
                }
                widthTh = (int)(widthOrig/f);
                heightTh = (int)(heightOrig/f);

               
                Bitmap thumbNailImg;
                thumbNailImg = new Bitmap(Convert.ToInt32(widthTh),Convert.ToInt32(heightTh));
                Graphics grfxThumb;
                grfxThumb = Graphics.FromImage(thumbNailImg);
             
               
                grfxThumb.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
               
                grfxThumb.DrawImage(uploadImage,new Rectangle(0,0,widthTh,heightTh));
               
                try{
                   
                    thumbNailImg.Save(sPath + newid + "." + fileext,theEncoder,myEncoderParameters);
                   
                }
                       
                catch(Exception ex)
                {
                    lblResults.Text = "Kan inte spara filen:" + ex.Message;
                    upload_ok = false;
                }
                finally{
                    txtUpload.FileContent.Close();
                }
               
            }
            else
            {
                try{
                    uploadImage.Save(sPath + newid + "." +fileext, theEncoder,myEncoderParameters);
                }
                catch(Exception ex)
                {
                    lblResults.Text = "Kan inte spara filen: " + ex.Message;
                    upload_ok = false;
                }
                finally{
                    txtUpload.FileContent.Close();
                }
            }

            if (upload_ok)
            {
                uploadImage.Save(sPath + @"/src/" + newid + "." + fileext, theEncoder, myEncoderParameters);
            }
           
        }
        catch(Exception ex)
        {
            lblResults.Text = "Kan inte spara filen: " + ex.Message;
            upload_ok = false;
        }
        finally{
            txtUpload.FileContent.Close();
        }


       
        if(upload_ok) {
            //bilden mottagen...
            ResultPanel.Visible = true;
            UploadPanel.Visible = false;
            backlink.HRef = "/adm/gallery/single.asp?imageID=" + newid;
            UploadedImage.ImageUrl = "/dotnet/image.aspx?img=" + newid + "&siteID=" + siteid;
        }
        else{
            //något blev fel
            lblResults.Font.Bold = true;
            lblResults.Visible = true;
        }

    }
Reply | Threaded
Open this post in threaded view
|

Re: The process cannot access...IIS7, same code works in IIS5

Dean Brettle
Administrator
Your first reference to txtUpload.FileContent is not guaranteed to be followed by a call to txtUpload.FileContent.Close().  I recommend changing txtUpload.FileContent.Length to txtUpload.ContentLength so that you don't unnecessarily open the stream.

--Dean