Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jan 17, 2011 @ 10:02
    Bo Damgaard Mortensen
    0

    Crashing when uploading media and debugging

    Hi all,

    I am trying to make a good old multiple image upload from the frontend where the client can specify how many FileUpload controls he/she wants (max 10 though)

    So far it seems to work, but when the number of FileUpload controls gets over five it crashes when hitting the save button. This is my UI:

    "Opret" is the save button ;)

    My code for adding the FileUpload controls:

    protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
    
            if (Session["noOfUploads"] != null)
            {
                int amountOfControls = Convert.ToInt32(Session["noOfUploads"]);
                for (int i = 0; i < amountOfControls; i++)
                {
                    FileUpload fUl = new FileUpload();
                    Label lblLineBreak = new Label();
                    lblLineBreak.Text = "<br />";
                    fUl.ID = i.ToString();
                    fUl.Visible = true;
                    pnlUploadControls.Controls.Add(fUl);
                    pnlUploadControls.Controls.Add(lblLineBreak);
                }
            }

    And this is where I save the images on the save button click event:

    List<Media> images = new List<Media>();
    
            foreach (Control ctrl in pnlUploadControls.Controls)
            {
                if (ctrl is FileUpload)
                {
                    FileUpload newFileUpload = (FileUpload)ctrl;
    
                    Media media = UmbracoSave(newFileUpload, storydDoc.Id);
                    if (media != null)
                    {
                        images.Add(media);
                    }
                }
            }
    
            if (images.Count > 0)
            {
                storydDoc.getProperty("forsidebillede").Value = images[0].getProperty("umbracoFile").Value;
                string xml = "<MultiNodeTreePicker>";
                foreach (Media img in images)
                {
                    xml += "<nodeId>" + img.Id + "</nodeId>";
                }
                xml += "</MultiNodeTreePicker>";
                storydDoc.getProperty("billeder").Value = xml;
            }    

    The id of the uploaded image is added to uComponents MultiNodeTreePicker as a XML value.

    So basically when the number of FileUpload controls is greater than five it crashes with the dreaded: "Server Error in '/' Application. Runtime Error" message, which kinda leads me to a side-question:

    How do you go about debugging usercontrols on a live site? If that is possible at all? I feel I have to guess what the error is everytime I develop a .net usercontrol. I have watched Niels' video about debugging and Attach to Process, but it doesn't work for me - not even on a local project :/ So how do you go about debugging your usercontrols?

    Any hint/help is greatly appreciated! :-)

    All the best,

    Bo

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jan 17, 2011 @ 16:52
    Matt Brailsford
    0

    Hey Bo,

    Locally I tend to use the attach to process technique to connect to the worker process for my given website which allows you to use breakpoints and step throughs etc.

    On the live server though, I tend to go back to old school, either writing variables out to the screen, or better, to the Umbraco log table.

    Would be interested to know if folks are doing anything differently myself.

    Matt

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jan 17, 2011 @ 17:27
    Bo Damgaard Mortensen
    0

    Hi Matt,

    Thanks for tuning in! 

    Yes, I've found myself debugging the old fashioned way by writing variables, response.redirect and so on ;) While it sort of works, it would be nice to be able to do some more "tracking"

    Anyway, the multiple file/image upload: I skipped the approach from my original post and tried using the jQuery Multiple File Upload 'framework', but it still crashes :( Even when trying to upload a single image. This is my code:

    protected List<Media> UploadImages()
        {
            List<Media> images = new List<Media>();
            HttpFileCollection hfc = Request.Files;
    
            foreach (HttpPostedFile file in hfc)
            {
                Media m = Media.MakeNew(
                    file.FileName, MediaType.GetByAlias("image"), User.GetUser(0), 1097);
    
                string mediaRootPath = "D:/web/localuser/mysite.comk/cms/media/";
                string storagePath = mediaRootPath + m.Id.ToString();
                System.IO.Directory.CreateDirectory(storagePath);
                string fullFilePath = storagePath + "\\" + file.FileName;
    
                file.SaveAs(fullFilePath);
    
                FileStream fs = new FileStream(fullFilePath,
                        FileMode.Open, FileAccess.Read, FileShare.Read);
    
                System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
    
                fs.Close();
    
                Bitmap bmp = Bitmap.FromFile(fullFilePath) as Bitmap;
                bmp = ResizeImage(bmp, 550, bmp.Height);
                bmp.Save(fullFilePath, ImageFormat.Jpeg);
    
                int fileWidth;
                int fileHeight;
    
                fileWidth = bmp.Width;
                fileHeight = bmp.Height;
    
                bmp.Dispose();
    
                string fileNameThumb = fullFilePath.Replace("." + file.ContentType, "_thumb");
                generateThumbnail(image, 100, fileWidth, fileHeight, fullFilePath, file.ContentType, fileNameThumb + ".jpg");
    
                m.getProperty("umbracoExtension").Value = file.ContentType;
                m.getProperty("umbracoBytes").Value = file.ContentLength;
                m.getProperty("umbracoHeight").Value = fileHeight;
                m.getProperty("umbracoWidth").Value = fileWidth;
    
                string mediaPath = "/media/" + m.Id.ToString() + "/" + file.FileName;
    
                m.getProperty("umbracoFile").Value = mediaPath;
                m.XmlGenerate(new XmlDocument());
    
                images.Add(m);
            }

    .. and I'm having a hard time tracing where the problem is. In google chrome the taskbar says "Uploading(x%) ... " but when it's done uploading, it crashes.

    Anyone got any experience working with the HttpFileCollection class to upload files to Umbraco?

    Thanks again!

    Bo

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jan 18, 2011 @ 09:33
    Matt Brailsford
    0

    Hey Bo,

    I can't say I can see anything glaringly wrong with your code. Are there any more specific errors in your servers event log?

    You are welcome to look through my code for uRest which is a set of web services for umbraco (currently unreleased) to see how I am handling uploading media files.

    http://urest4umb.codeplex.com/SourceControl/list/changesets

    Matt

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jan 18, 2011 @ 14:14
    Bo Damgaard Mortensen
    0

    Hi Matt,

    Thanks for your link :)

    I got it to work last night by reading one of your old posts here: changing "umbracoExtensio" to "umbracoExtension" did the trick! 

Please Sign in or register to post replies

Write your reply to:

Draft