public string fileSaveAs(HttpPostedFile _postedFile, int _isWater)
{
try
{
string _fileExt = _postedFile.FileName.Substring(_postedFile.FileName.LastIndexOf(".") + 1);
//验证合法的文件
if (!CheckFileExt(this.fileType, _fileExt))
{
return "{msg: 0, msbox: \"不允许上传" + _fileExt + "类型的文件!\"}";
}
if (this.fileSize > 0 && _postedFile.ContentLength > fileSize * 1024)
{
return "{msg: 0, msbox: \"文件超过限制的大小!\"}";
}
string _fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "." + _fileExt; //随机文件名
//检查保存的路径 是否有/开头结尾
if (this.filePath.StartsWith("/") == false) this.filePath = "/" + this.filePath;
if (this.filePath.EndsWith("/") == false) this.filePath = this.filePath + "/";
//按日期归类保存
string _datePath = DateTime.Now.ToString("yyyyMMdd") + "/";
this.filePath += _datePath;
//获得要保存的文件路径
string serverFileName = this.filePath + _fileName;
//物理完整路径
string toFileFullPath = HttpContext.Current.Server.MapPath(this.filePath);
//检查是否有该路径没有就创建
if (!Directory.Exists(toFileFullPath))
{
Directory.CreateDirectory(toFileFullPath);
}
//将要保存的完整文件名
string toFile = toFileFullPath + _fileName;
//保存文件
_postedFile.SaveAs(toFile); return "{msg: 1, msbox: \"" + serverFileName + "\"}";
}
catch
{
return "{msg: 0, msbox: \"上传过程中发生错误!\"}";
}
}
{
try
{
string _fileExt = _postedFile.FileName.Substring(_postedFile.FileName.LastIndexOf(".") + 1);
//验证合法的文件
if (!CheckFileExt(this.fileType, _fileExt))
{
return "{msg: 0, msbox: \"不允许上传" + _fileExt + "类型的文件!\"}";
}
if (this.fileSize > 0 && _postedFile.ContentLength > fileSize * 1024)
{
return "{msg: 0, msbox: \"文件超过限制的大小!\"}";
}
string _fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "." + _fileExt; //随机文件名
//检查保存的路径 是否有/开头结尾
if (this.filePath.StartsWith("/") == false) this.filePath = "/" + this.filePath;
if (this.filePath.EndsWith("/") == false) this.filePath = this.filePath + "/";
//按日期归类保存
string _datePath = DateTime.Now.ToString("yyyyMMdd") + "/";
this.filePath += _datePath;
//获得要保存的文件路径
string serverFileName = this.filePath + _fileName;
//物理完整路径
string toFileFullPath = HttpContext.Current.Server.MapPath(this.filePath);
//检查是否有该路径没有就创建
if (!Directory.Exists(toFileFullPath))
{
Directory.CreateDirectory(toFileFullPath);
}
//将要保存的完整文件名
string toFile = toFileFullPath + _fileName;
//保存文件
_postedFile.SaveAs(toFile); return "{msg: 1, msbox: \"" + serverFileName + "\"}";
}
catch
{
return "{msg: 0, msbox: \"上传过程中发生错误!\"}";
}
}
<asp:TextBox ID="txtUrl" runat="server" CssClass="input w380 left"></asp:TextBox>
<a href="javascript:void(0);" class="files">
<input type="file" id="FileUpload" name="FileUpload" /></a> //js代码
//单个文件上传
$(function () {
$("#FileUpload").bind("change", function () {
//开始提交
$("#form1").ajaxSubmit({
beforeSubmit: function (formData, jqForm, options) {
//隐藏上传按钮
$(".files").hide();
//显示LOADING图片
$(".uploading").show();
},
success: function (data, textStatus) {
if (data.msg == 1) {
$("#txtUrl").val(data.msbox);
} else {
alert(data.msbox);
}
$(".files").show();
$(".uploading").hide();
},
error: function (data, status, e) {
alert("上传失败,错误信息:" + e);
$(".files").show();
$(".uploading").hide();
},
url: "/Tools/SingleUpload.ashx",
type: "post",
dataType: "json",
timeout: 600000
});
});
});
//SingleUpload.ashx代码
public class SingleUpload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//检查是否登录后上传操作
//if (context.Session["AdminNo"] == null)
//{
// context.Response.Write("{msg: 0, msbox: \"请登录后再进行上传文件!\"}");
// return;
//}
HttpPostedFile _upfile = context.Request.Files["FileUpload"];
string _delfile = context.Request.Params["txtUrl"];
if (_upfile == null)
{
context.Response.Write("{msg: 0, msbox: \"请选择要上传文件!\"}");
return;
}
UpLoad upFiles = new UpLoad();
string msg = upFiles.FileSaveAs(_upfile, 0);
//删除已存在的旧文件
if (!string.IsNullOrEmpty(_delfile))
{
string _filename = HttpContext.Current.Server.MapPath(_delfile);
if (File.Exists(_filename))
{
File.Delete(_filename);
}
}
//返回成功信息
context.Response.Write(msg);
} public bool IsReusable
{
get
{
return false;
}
}
}
//upload文件上传代码
public string FileSaveAs(HttpPostedFile _postedFile, int _isWater)
{
try
{
string _fileExt = _postedFile.FileName.Substring(_postedFile.FileName.LastIndexOf(".") + 1);
//验证合法的文件
if (!CheckFileExt(this.fileType, _fileExt))
{
return "{msg: 0, msbox: \"不允许上传" + _fileExt + "类型的文件!\"}";
}
if (this.fileSize > 0 && _postedFile.ContentLength > fileSize * 1024)
{
return "{msg: 0, msbox: \"文件超过限制的大小!\"}";
}
string _fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "." + _fileExt; //随机文件名
//检查保存的路径 是否有/开头结尾
if (this.filePath.StartsWith("/") == false) this.filePath = "/" + this.filePath;
if (this.filePath.EndsWith("/") == false) this.filePath = this.filePath + "/";
//按日期归类保存
string _datePath = DateTime.Now.ToString("yyyyMMdd") + "/";
this.filePath += _datePath;
//获得要保存的文件路径
string serverFileName = this.filePath + _fileName;
//物理完整路径
string toFileFullPath = HttpContext.Current.Server.MapPath(this.filePath);
//检查是否有该路径没有就创建
if (!Directory.Exists(toFileFullPath))
{
Directory.CreateDirectory(toFileFullPath);
}
//将要保存的完整文件名
string toFile = toFileFullPath + _fileName;
//保存文件
_postedFile.SaveAs(toFile); return "{msg: 1, msbox: \"" + serverFileName + "\"}";
}
catch
{
return "{msg: 0, msbox: \"上传过程中发生错误!\"}";
}
}