自己写的;
//保存上传图片路径到Cookie
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = BLL.Member.ME_MembersInfo.ReturnPhotoCode();
string PathExtension = "";
string Path = "";
Common.Cookie.CookieClear();
if (FileUpload1.HasFile)
{
int filelength = FileUpload1.PostedFile.ContentLength;
if (filelength > 30720)
{
Common.Alert.Show(this, "上传相片不能大于30KB!");
return;
}
string filename = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName);
PathExtension = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName.Trim());
if (PathExtension.Equals(".jpg"))
{
if (Convert.ToInt32(DateTime.Now.Month) < 10)
{
Path = "~/upload/" + Convert.ToString(DateTime.Now.Year) + "/0" + Convert.ToString(DateTime.Now.Month);
}
else
{
Path = "~/upload/" + Convert.ToString(DateTime.Now.Year) + "/" + Convert.ToString(DateTime.Now.Month);
}
}
else
{
Common.Alert.Show(this, "请选择.jpg格式");
return;
}
if (!Directory.Exists(Server.MapPath(Path)))
{
Directory.CreateDirectory(Server.MapPath(Path));
}
string SaveSql = Path + "/" + filename.Replace(filename, dt.Rows[0][0].ToString()) + PathExtension;
Common.Cookie.SetCookie("PQ_Path", SaveSql);
string Lpath = Server.MapPath(Path) + "/" + filename.Replace(filename, dt.Rows[0][0].ToString()) + PathExtension;
FileUpload1.PostedFile.SaveAs(Lpath);
Common.Alert.Show(this, "上传成功");
}
else
{
Common.Alert.Show(this,"请选择.jpg图片");
return;
}
}
网上找的,值得参考下;
Boolean fileOk = false;
string path = Server.MapPath("~/upload/");
if (FileUpload1.HasFile)
{
//获取扩展名
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
//限定只能上传jpg和gif图片
string[] allowExtension = { ".jpg", ".gif", ".jpeg", ".bmp" };
for (int i = 0; i < allowExtension.Length; i++)
{
if (fileExtension == allowExtension[i])
{
fileOk = true;
break;
}
}
if (fileOk == false)
{
ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "提示!", "alert('文件类型错误!');", true);
}
//对上传文件的大小进行检测,限定文件最大不超过1M
if (FileUpload1.PostedFile.ContentLength > 1024000)
{
fileOk = false;
}
if (fileOk)
{
try
{
string pat = path + ReName(fileExtension);
ct.CardImage ="upload/" + ReName(fileExtension);
B2CBll.CardTypeManager.AddCardType(ct);
FileUpload1.PostedFile.SaveAs(pat);
ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "提示!", "alert('添加成功!');", true);
this.BindListBox();
}
catch
{
ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "提示!", "alert('上传失败!');", true);
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "提示!", "alert('文件类型或者文件大小超出1M或者文件类型不对');", true);
}
}
}
protected void BtnFileUpload_Click(object sender, EventArgs e) { DataTable dt = getCode(); if (FileUpload1.HasFile) { bool test = false; string path = ""; string fullPath = ""; string fileName = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName); string filexls = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName); double fileLenght = (Convert.ToDouble(FileUpload1.PostedFile.ContentLength) / Convert.ToDouble(1024)); string[] strs = { ".jpg", ".jpeg", ".gif", ".rar", ".txt", ".bng", ".doc" }; for (int i = 0; i < strs.Length; i++) { if (filexls.ToLower() == strs[i].ToLower()) { test = true; break; } } if (test) { try { path = "~/upload/" + DateTime.Now.Year + "/" + DateTime.Now.Month; fullPath = Server.MapPath(path); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } fullPath = Server.MapPath(path) + "/" + fileName.Replace(fileName, dt.Rows[0][0].ToString()) + filexls; path = "~/upload/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + fileName.Replace(fileName, dt.Rows[0][0].ToString()) + filexls; setCookie("filename", fileName); setCookie("filexls", filexls); setCookie("fileLength", fileLenght.ToString()); setCookie("Path", path); FileUpload1.PostedFile.SaveAs(fullPath); AddPhotoTest(); GetPhototest(); } catch (Exception ex) { ScriptManager.RegisterClientScriptBlock(FileUpload1, GetType(), "提示", "alert('上传文件出现异常');", true); return; } } else { ScriptManager.RegisterClientScriptBlock(FileUpload1, GetType(), "提示", "alert('上传文件不支持此格式');", true); return; } ScriptManager.RegisterClientScriptBlock(FileUpload1, GetType(), "提示", "alert('上传成功');", true); } else { ScriptManager.RegisterClientScriptBlock(FileUpload1, GetType(), "提示信息", "alert('请选择上传文件');", true); return; } }
本段代码资料来源(百度知道):
资料来源:http://zhidao.baidu.com/question/111503334.html
求asp.net文件上传下载代码,不需要写那么详细(本人不是菜鸟,给出关键步骤,但贴到工程里就可以使用才行),我写的最大只能上传4M,不知道为什么
要求至少可以上传200M,自己试过了再发上来
Web.Config
<!--文件上传的大小限制:200M(200000KB),执行最大时限是100分钟(6000秒)-->
<httpRuntime maxRequestLength="200000" executionTimeout="6000"/>
本段代码资料来源(百度知道):
浙公网安备 33010602011771号