Search code examples
c#asp.netfile-uploadmultifile-uploader

I can't use fileUpload.PostedFiles[] property?


I can't use fileUpload.PostedFiles[] property?
I actually need a multiple fileuploader & resize.
I use HttpFileCollection hfc for all my files. It go's always wrong by string filename = fileUpload.
Is this a good way to do this?

protected void UploadFile_Click(object sender, EventArgs e)
    {
        HttpFileCollection hfc = Request.Files;
        for (int i = 0; i < hfc.Count; i++)
        {
            HttpPostedFile hpf = hfc[i];
            if (hpf.ContentLength > 0)
            {
              if (fileUpload.HasFile)
                {

                    **string filename=fileUpload.PostedFiles[i].FileName;**    

                    string directory = Server.MapPath(@"Uploaded-Files\");    

                    Bitmap originalBMP = new Bitmap(fileUpload.FileContent);

                    Bitmap newBMP = new Bitmap(originalBMP, 800, 480);

                    Graphics oGraphics = Graphics.FromImage(newBMP);   

                    oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);

                    newBMP.Save(directory + "tn_" + filename);

                    originalBMP.Dispose();
                    newBMP.Dispose();
                    oGraphics.Dispose();

                }

            }

        }



    }

Solution

  • You need to do this:

    HttpFileCollection multipleFiles = Request.Files;
    for (int fileCount = 0; fileCount < multipleFiles.Count; fileCount++) 
    {
        HttpPostedFile uploadedFile = multipleFiles[fileCount];
        string fileName = Path.GetFileName(uploadedFile.FileName);
        if (uploadedFile.ContentLength > 0) {
            uploadedFile.SaveAs(Server.MapPath("~/Files/") + fileName);
            Label1.Text += fileName + "Saved <BR>";
        }
    }
    

    Here's the Re-size method:

    public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
    {
        System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);
    
        // Prevent using images internal thumbnail
        FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
        FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    
        if (OnlyResizeIfWider)
        {
            if (FullsizeImage.Width <= NewWidth)
            {
                NewWidth = FullsizeImage.Width;
            }
        }
    
        int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
        if (NewHeight > MaxHeight)
        {
            // Resize with height instead
            NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
            NewHeight = MaxHeight;
        }
    
        System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
    
        // Clear handle to original file so that we can overwrite it if necessary
        FullsizeImage.Dispose();
    
        // Save resized picture
        NewImage.Save(NewFile);
    }
    

    Reference: http://www.dzone.com/snippets/c-resize-image-while