前台写了个FileUpload和Button,后台怎么写?
解决方案 »
- iis连接网页失败
- ASP.NET读数据库偶尔发生internal connection fatal error 错误
- 关于抓取.aspx页面内的datagridview数据问题
- Gridview更新按钮未激发RowCommond事件
- 图片上传出现奇怪问题,分全部送出~!
- DataGrid模板列绑定图片
- 我想把一个access表连到DataGrid,不写代码,只用拖放组件的形式(演示给别人看),步骤是什么啊?
- 请问怎样获得表中每一个类别的最新的一条记录?
- 如何防止多人写入数据库的冲突
- 求助:一个含有reportviewer控件的usercontrol在本地运行好用,但加到sharepoint 2003上就报错。
- windows 8批处理 显示时间
- 后台上传文档,不限类型(doc,excel,txt都可以),前台下载的时候转换为PDF
protected void btnOK_Click(object sender, EventArgs e)
{
//上传列表图片
try
{
string datedir = DateTime.Now.ToString("yyyyMMdd");
string name = Maticsoft.Utility.Tool.Upload(myFileUpload, new string[] { ".jpg", ".png", ".gif" }, 3, Server.MapPath("../download/"));
}
catch (Exception ex)
{
//错误 }
}
傻了。竟然忘记贴函数了。SORRY public static string Upload(FileUpload myFileUpload, string[] allowExtensions, int maxLength, string savePath)
{
// 文件格式是否允许上传
bool fileAllow = false; //检查是否有文件案
if (myFileUpload.HasFile)
{
// 检查文件大小, ContentLength获取的是字节,转成M的时候要除以2次1024
if (myFileUpload.PostedFile.ContentLength / 1024 / 1024 >= maxLength)
{
throw new Exception("只能上传小于" + maxLength + "M的文件!");
} //取得上传文件之扩展文件名,并转换成小写字母
string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
string tmp = ""; // 存储允许上传的文件后缀名
//检查扩展文件名是否符合限定类型
for (int i = 0; i < allowExtensions.Length; i++)
{
tmp += i == allowExtensions.Length - 1 ? allowExtensions[i] : allowExtensions[i] + ",";
if (fileExtension == allowExtensions[i])
{
fileAllow = true;
}
} if (fileAllow)
{
try
{
string datedir = DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(savePath + datedir))
{
Directory.CreateDirectory(savePath + datedir);
}
string saveName = Guid.NewGuid() + fileExtension;
string path = savePath + datedir + "/" + saveName;
//存储文件到文件夹
myFileUpload.SaveAs(path);
return datedir + "/" + saveName;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
else
{
throw new Exception("文件格式不符,可以上传的文件格式为:" + tmp);
}
}
else
{
throw new Exception("请选择要上传的文件!");
}
}
{
string uploadName = InputFile.Value;//获取待上传图片的完整路径,包括文件名
//string uploadName = InputFile.PostedFile.FileName;
string pictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复
if (InputFile.Value != "")
{
int idx = uploadName.LastIndexOf(".");
string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名
pictureName = DateTime.Now.Ticks.ToString() + suffix;
}
try
{
if (uploadName != "")
{
string path = Server.MapPath("~/images/");
InputFile.PostedFile.SaveAs(path + pictureName);
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
{
string uploadName = InputFile.Value;//获取待上传图片的完整路径,包括文件名
//string uploadName = InputFile.PostedFile.FileName;
string pictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复
if (InputFile.Value != "")
{
int idx = uploadName.LastIndexOf(".");
string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名
pictureName = DateTime.Now.Ticks.ToString() + suffix;
}
try
{
if (uploadName != "")
{
string path = Server.MapPath("~/images/");
InputFile.PostedFile.SaveAs(path + pictureName);
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}