1

我不能使用fileUpload.PostedFiles[]财产?
我实际上需要一个多文件上传器和调整大小。
我对所有文件都使用HttpFileCollectionhfc。它总是错的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();

                }

            }

        }



    }
4

2 回答 2

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

这是重新调整大小的方法:

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

于 2013-02-19T10:49:55.630 回答
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>");
于 2017-02-15T13:00:24.330 回答