//这是我的代码 进程上传文件图片,文件大小,没有文件相同
HttpFileCollection hfc = Request.Files;
if (hfc != null)
{
string cekDir = string.Format("{0}\\{1}", ConfigurationManager.AppSettings["docLoc"], id_hazard_report);
string PicDir;
if (Directory.Exists(cekDir)) //check Folder avlalible or not
{
PicDir = cekDir;
}
else
{
DirectoryInfo di = Directory.CreateDirectory(cekDir); // create Folder
PicDir = cekDir;
}
string fullname;
string filename;
//FileUpload FileUpload1 = (FileUpload)FormView1.FindControl("FileUpload1");
// string fileExt = Path.GetExtension(FileUpload1.FileName); //Get The File Extension
//此流程循环文件上传和我的问题
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
string fileExt = Path.GetExtension(hpf.FileName);
if (((CustomControls_DdlLocation)FormView1.Controls[0].FindControl("ddl_location1")).SelectedText.ToLower().Trim() == "kelanis")
{
// This my problem if file is empty...
if (string.IsNullOrEmpty(hpf.FileName))
{
myfb._error("Upload harus isi");
return;
if (hpf.ContentLength > 0)
{
///full path name to check exist or not
fullname = string.Format("{0}\\{1}", PicDir, Path.GetFileName(hpf.FileName.Replace(" ", "_")));
bool ex = File.Exists(fullname);
if (fileExt == (".jpg") || fileExt == (".gif") || fileExt == (".bmp") || fileExt == (".png") || fileExt == (".jpeg"))
{
if (hpf.ContentLength > 200000)/*(hpf.ContentLength > 200000)*/
{
//Session["rahmat"] = "errorLebihBesar";
// Response.Redirect(Request.RawUrl);
//ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('File Tidak boleh lebih dari 200 kb');</script>");
continue;
}
if (ex == true)
{
string f = Path.GetFileName(hpf.FileName.Replace(" ", "_"));
string[] a = new string[1];
a = f.Split('.');
filename = string.Format("{0}_{1}.{2}", a.GetValue(0), DateTime.Now.ToString("yymdHm"), a.GetValue(1));
}
else
{
filename = Path.GetFileName(hpf.FileName.Replace(" ", "_")).ToString();
}
///full path name to store in database with new filename
//string[] aa = new string[1];
//filename = string.Format("{0}_{1}.{2}", aa.GetValue(0), DateTime.Now.ToString("yymdHm"), aa.GetValue(1));
fullname = string.Format("{0}\\{1}", PicDir, filename);
hpf.SaveAs(fullname);
InsertHazardDoc(id_hazard_report, filename);
}
else
{
//Session["rahmat"] = "errorFormat";
//Response.Redirect(Request.RawUrl);
// FileUpload1.Focus();
// ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('File Bukan Format Gambar');</script>");
continue;
}
}
}
}
}
}
//Page.DataBind();
//myfb._success("Hazard Report Succesfully Inserted");
Session["rahmat"] = "success";
Response.Redirect(Request.RawUrl);
}
catch (Exception ex)
{
myfb._error(ex.ToString());
}
可以帮帮我吗?如何解决这个问题。如果我上传文件为空是真的显示消息错误像这样myfb._error(&#34;上传harus isi&#34;);但如果我上传文件仍然显示消息错误myfb._error(&#34;上传harus isi&#34;);.我想如果有上传文件继续focesffull
答案 0 :(得分:1)
我不知道如何使用HttpFileCollection hfc = Request.Files; 如果您从表单发布,您可以这样使用。 需要改变
HttpFileCollection to System.Web.HttpFileCollectionBase and
System.Web.HttpPostedFileBase p;
foreach (System.Web.HttpPostedFileBase item in hfc)
{
p = item;
}
在客户端页面检查发布方法enctype是否存在
enctype="multipart/form-data"
如果使用服务器映像选择文件数据类型。