我不能使用fileUpload.PostedFiles []属性?

时间:2013-02-19 10:45:27

标签: c# asp.net file-upload multifile-uploader

我不能使用fileUpload.PostedFiles[]属性?
我实际上需要一个多文件上传器和放大器调整。
我为所有文件使用HttpFileCollection hfc。 string filename = fileUpload总是出错 这是一个很好的方法吗?

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

                }

            }

        }



    }

2 个答案:

答案 0 :(得分:2)

你需要这样做:

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>";
    }
}

这是Re-size方法:

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

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

答案 1 :(得分:1)

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>");
相关问题