Question

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();

                }

            }

        }



    }
Was it helpful?

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

OTHER TIPS

int fileCount;
            HttpFileCollection multipleFiles = Request.Files;
            for (fileCount = 0; fileCount < multipleFiles.Count; fileCount++)
            {
                HttpPostedFile uploadedFile = multipleFiles[fileCount];

                SqlCommand cmdinsert = new SqlCommand("insert into tb_gallery values(@image,@file_upload)", cn);
                cmdinsert.Parameters.AddWithValue("@image", DropDownList1.SelectedItem.Text);

                string imgname = "";
                Random random = new Random();
                imgname = "wc" + random.Next(10, 200000).ToString() + uploadedFile.FileName.ToString();
                uploadedFile.SaveAs(Server.MapPath("../DataFilesTemp/" + imgname));
                mc.ImgCompress(imgname, "Gallery", 900, 550);
                cmdinsert.Parameters.AddWithValue("@file_upload", imgname);

                cmdinsert.ExecuteNonQuery();

            }
            Response.Write("<script>alert('"+fileCount+" files uploaded successfuly')</script>");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top