Welcome Guest Search | Active Topics | Members

ASP.NET 2.0 Options
zoki_57
Posted: Friday, August 18, 2006 1:45:01 AM
Rank: Member
Groups: Member

Joined: 4/12/2006
Posts: 6
Points: 0
Hi Fedor

We are migrating our application from .NET 1.1 to .NET 2.0
The ImageUploader component worked fine in the previous version but in the new version when I try to upload large images the application is throwing error:

Code:
ex.Message: Cannot access a closed file.
e.StackTrace:
   at System.IO.__Error.FileNotOpen()
   at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)
   at System.Web.HttpRawUploadedContent.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer, Int32  bufferOffset)
   at System.Web.HttpRawUploadedContent.WriteBytes(Int32 offset, Int32 length, Stream stream)
   at System.Web.HttpInputStream.WriteTo(Stream s)
   at System.Web.HttpPostedFile.SaveAs(String filename)
   at korthuset_net.aspSmartUpload1.uploadLogics(String dirPath, HttpPostedFile sourceFile, HttpPostedFile thumbFile) in d:\\Projects\\KorthusetWeb-TESTING\\aspSmartUpload1.aspx.cs:line 236"



The code that we use is:

Code:
protected override void Page_Load(object sender, System.EventArgs e)
{
      base.Page_Load (sender, e);
                  
      //Get total number of uploaded files
      int intFileCount = Int32.Parse(Request.Form["FileCount"]);

      string strGalleryPath = @"C:\\Gallery"
      if(!Directory.Exists(strGalleryPath ))
            Directory.CreateDirectory(strGalleryPath );
      if(!Directory.Exists(strGalleryPath + "\\Thumbnails"))
            Directory.CreateDirectory(strGalleryPath + "\\Thumbnails");


      dirPath = strGalleryPath +"\\";

      //We iterate through the uploaded files and save them and appropriate data
      for (int i = 1; i <= intFileCount; i++)
      {
         //Get source image and save it to disk
                sourceFile = Request.Files["SourceFile_1"];
                thumbFile = Request.Files["Thumbnail1_1"];

                tr = new Thread(new ThreadStart(this.NewThread));
                tr.Start();
             }
}

private void NewThread()
{
            try
            {
                uploadLogics(dirPath, sourceFile, thumbFile);
            }
            catch (Exception ex)
            {
                string exp = ex.Message;
            }
            finally
            {
                tr.Abort();
            }

        }

        private void uploadLogics(string dirPath, HttpPostedFile sourceFile, HttpPostedFile thumbFile)
        {
            string fileName;
            string ext;
            long size;

            //get file name
            fileName = Path.GetFileName(sourceFile.FileName);

            ext = fileName.Substring(fileName.LastIndexOf("."));
            fileName = fileName.Substring(0, fileName.LastIndexOf("."));

            //if file is tif it will be saved as .jpg
            bool tif = false;
            if (ext.ToLower().Equals(".tif") || ext.ToLower().Equals(".tiff"))
            {
                ext = ".jpg";
                tif = true;
            }

            fileName = Regex.Replace(fileName, @"[ ',;+%]", "_");

            //if already was uploaded file with that name, the current file
            string newFileName = fileName + ext;
            int j = 1;
            while (File.Exists(dirPath + newFileName))
            {
                newFileName = j + "_" + fileName + ext;
                j++;
            }
            ext = newFileName.Substring(newFileName.LastIndexOf("."));
            fileName = newFileName.Substring(0, newFileName.LastIndexOf("."));

            //shrink the file name to max 50 characters
            fileName = fileName.Length > 50 ? fileName.Substring(0, 50) : fileName;

            //save file
            try
            {
                if (tif)
                {
                    ext = ".jpg";

                    System.Drawing.Image im = System.Drawing.Image.FromStream(sourceFile.InputStream);
                    im.Save(dirPath + fileName + ".jpg", ImageFormat.Jpeg);
                    im.Dispose();
                }
                else
                {
                    sourceFile.SaveAs(dirPath + fileName + ext);
                }
                size = sourceFile.InputStream.Length;
                //Get first thumbnail (the single thumbnail in this code sample)
                thumbFile.SaveAs(dirPath + "Thumbnails\\" + fileName + ext);

                //database insert code.....
            }
            catch (Exception ex)
            {
                 string message = ex.Message;
                 string stack = ex.StackTrace;
            }
}



The ImageUploader is configured like this:

Code:
var iu = new ImageUploaderWriter("ImageUploader", 650, 350);
iu.activeXControlEnabled = true;
iu.javaAppletEnabled = true;
iu.activeXControlCodeBase = "ImageUploader4.cab";
iu.javaAppletCodeBase="./";

iu.addParam("PaneLayout", "TwoPanes");
iu.addParam("BackgroundColor", "#ffffff");
iu.addParam("UploadThumbnail1FitMode", "Fit");
iu.addParam("UploadThumbnail1Width", "120");
iu.addParam("UploadThumbnail1Height", "120");
iu.addParam("UploadThumbnail1JpegQuality", "60");
iu.addParam("FilesPerOnePackageCount", "1");
iu.addParam("AutoRecoverMaxTriesCount", "5");
iu.addParam("AutoRecoverTimeOut", "1200");
iu.addParam("ShowDebugWindow", "True");
iu.addParam("AdditionalFormName", "Form1");
iu.addParam("AllowRotate", "False");
iu.addParam("Action", "aspSmartUpload1.aspx");
iu.addParam("FileMask", "*.jpg;*.bmp;*.tiff;*.tif");
iu.addParam("MaxFileSize", "5242880");
iu.addParam("ButtonStopText", "<%=languageText["bStop"]%>");
iu.addParam("ShowDescriptions", "0");
iu.addParam("CheckFilesBySelectAllButton", "True");
iu.addEventListener("Progress", "ImageUploaderID_Progress");

iu.writeHtml();
            
function ImageUploaderID_Progress(Status, Progress, ValueMax, Value, StatusText)
{
      if (Status=="COMPLETE")
      {
            tt = window.setTimeout("doAction()",2000);
      }
      if (Status=="CANCEL")
      {
            tt = window.setTimeout("doAction()",2000);
      }
}



The problem is occuring only with large images like images with sizes > 3MB for example, but with small size images 20, 50, 90 KB everything is OK

Can you please help me with this problem ASAP?

Thanks in advance
Zoki
Alex Makhov
Posted: Friday, August 18, 2006 4:23:49 AM

Rank: Advanced Member
Groups: Member

Joined: 8/3/2003
Posts: 996
Points: 1
Hello,

Please see new ASP.NET 2.0 RequestLengthDiskThreshold property of HttpRuntime web.config section. You should set appropriate value for your purposes (for example to 8192). It seems that ASP.NET 2.0 allows about 200kb temporary files only. Also you should set Page.EnableEventValidation property to False value.

Please see the discussion of the same issue on ASP.NET Forums.

Sincerely yours,
Alex Makhov
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Yet Another Forum.net version 1.9.1.6 running under Cuyahoga.
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.