我可以';t使用fileUpload.PostedFiles[]属性
本文关键字:PostedFiles 属性 fileUpload 使用 我可以 | 更新日期: 2023-09-27 18:00:16
我不能使用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();
}
}
}
}
您需要这样做:
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
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>");