小文件上传代码
2011-09-20 14:31
197 查看
前台:
<input type="file" runat="server" id="oFile" /><a runat="server" onclick="if(document.getElementById('oFile').value==''){alert('请先选择文件!');return false;}" onserverclick="Upfile_Click" href="#">上传图片</a>
后台:
protected void Upfile_Click(object sender, EventArgs e)
{
HttpPostedFile hpf = oFile.PostedFile;
if (hpf.ContentLength > 3 * 1024 * 1024)
{
MsgShow("很抱歉,上传的图片不能超过3M!");
return;
}
string extOnly="jpg|png|bmp|gif";
if (!CheckExtValid(hpf.FileName, extOnly))
{
MsgShow("文件类型限定为:" + extOnly.Replace("|", "、"));
return;
}
//设置服务器文件路径
string dFile = SiteConfig.ImgMap +"\\images\\upload\\";
Sxmobi.FileHelper.EnsureDir(dFile);
//dFile += Path.GetFileName(hpf.FileName); //原文件名
dFile += DateTime.Now.ToString("yyyyMMddHHmmssfff")+ Path.GetExtension(hpf.FileName);
hpf.SaveAs(dFile);
MsgShow("上传成功!");
}
bool CheckExtValid(string filename, string extOnly)
{
string strExt = Path.GetExtension(filename).ToUpper();
bool bOK = true;
if (extOnly != "")
{
bOK = false;
string[] arrOnly = extOnly.Split('|');
for (int i = 0; i < arrOnly.Length; i++)
{
if (strExt == "." + arrOnly[i].ToUpper())
bOK = true;
}
}
return bOK;
}
void MsgShow(string msg)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + msg + "')</script>");
}
转载请注明出处:http://blog.csdn.net/dasihg/article/details/6793656
<input type="file" runat="server" id="oFile" /><a runat="server" onclick="if(document.getElementById('oFile').value==''){alert('请先选择文件!');return false;}" onserverclick="Upfile_Click" href="#">上传图片</a>
后台:
protected void Upfile_Click(object sender, EventArgs e)
{
HttpPostedFile hpf = oFile.PostedFile;
if (hpf.ContentLength > 3 * 1024 * 1024)
{
MsgShow("很抱歉,上传的图片不能超过3M!");
return;
}
string extOnly="jpg|png|bmp|gif";
if (!CheckExtValid(hpf.FileName, extOnly))
{
MsgShow("文件类型限定为:" + extOnly.Replace("|", "、"));
return;
}
//设置服务器文件路径
string dFile = SiteConfig.ImgMap +"\\images\\upload\\";
Sxmobi.FileHelper.EnsureDir(dFile);
//dFile += Path.GetFileName(hpf.FileName); //原文件名
dFile += DateTime.Now.ToString("yyyyMMddHHmmssfff")+ Path.GetExtension(hpf.FileName);
hpf.SaveAs(dFile);
MsgShow("上传成功!");
}
bool CheckExtValid(string filename, string extOnly)
{
string strExt = Path.GetExtension(filename).ToUpper();
bool bOK = true;
if (extOnly != "")
{
bOK = false;
string[] arrOnly = extOnly.Split('|');
for (int i = 0; i < arrOnly.Length; i++)
{
if (strExt == "." + arrOnly[i].ToUpper())
bOK = true;
}
}
return bOK;
}
void MsgShow(string msg)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + msg + "')</script>");
}
转载请注明出处:http://blog.csdn.net/dasihg/article/details/6793656
相关文章推荐
- 文件上传时生成“日期+随机数”式文件名前缀的Java代码
- php 生成自动创建文件夹并上传文件的示例代码
- android 上传文件到服务器代码实例
- Servlet+Jsp实现图片或文件的上传功能具体思路及代码
- C#代码上传文件到SharePoint文档库并给字段赋值
- 网站的文件的上传,并将相对路径保存到数据库的代码实现。
- JavaScript限制上传文件类型的代码
- 关于 PHP 文件上传错误代码 UPLOAD_ERR_PARTIAL: 3 解决办法
- php实现上传图片文件代码
- android上传图片、视频、大文件代码
- Spring MVC 文件上传的示例代码
- ASP.NET 多文件上传代码
- 压缩文件上传,然后再解压。这是一段有bug的代码,供自己以后需求参考
- jsp文件上传与下载实例代码
- 购物网站核心代码_文件上传_发送邮件_购物车设计_在线支付
- Apache Http Client 4 上传多个文件 (示例代码可在 github 上找到)
- android 上传文件到服务器代码实例
- webuploader在springMVC+jquery+Java开发环境下的大文件分片上传的实例代码
- ssh框架实现文件上传下载实例代码