日期:2014-05-18 浏览次数:20616 次
function UploadFile() {
//准备提交
//$("#btnSave").attr("disabled", 'disabled');
var result = 0;
$.ajax({
type: 'POST',
url: '../../Action/UploadFile.ashx',
data: 'path=' + $("#fileUpload").val() + '&act=add&type=4',
success: function(data, status) {
var stringArray = data.split("|");
if (stringArray[0] == "1") {
//stringArray[0] 成功状态(1为成功,0为失败)
//stringArray[1] 上传成功的文件名
//stringArray[2] 消息提示
result = 1;
}
else {
//上传出错
alert("上传错误:" + stringArray[2]);
//$("#btnSave").removeAttr("disabled");
}
//$("#loading_msg").html("");
},
error: function(data, status, e) {
alert("上传失败:" + e.toString());
//$("#btnSave").removeAttr("disabled");
//return false;
}
});
//$("#btnSave").removeAttr("disabled");
return (result == 0) ? false : true;
}
//获取要保存的文件信息
System.IO.FileInfo file = new System.IO.FileInfo(fileNamePath);
//获得文件扩展名
string fileNameExt = file.Extension;
_old_file_name = file.Name;
//验证合法的文件
if (CheckFileExt(fileNameExt.ToLower()))
{
_new_file_id = GetFileName();
//生成将要保存的随机文件名
string fileName = _new_file_id + fileNameExt;
//_new_file_id = fileName;
//检查保存的路径 是否有/结尾
if (toFilePath.EndsWith("/") == false) toFilePath = toFilePath + "/";
//按日期归类保存
string datePath = DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("dd") + "/";
if (true)
{
toFilePath += datePath;
}
//获得要保存的文件路径
string serverFileName = toFilePath + fileName;
_new_file_path = serverFileName;
//物理完整路径
string toFileFullPath = HttpContext.Current.Server.MapPath(toFilePath);
//检查是否有该路径 没有就创建
if (!System.IO.Directory.Exists(toFileFullPath))
{
System.IO.Directory.CreateDirectory(toFileFullPath);
}
//将要保存的完整文件名
string toFile = toFileFullPath + fileName;
//附件信息添加到数据库
///创建WebClient实例
System.Net.WebClient myWebClient = new System.Net.WebClient();
//设定windows网络安全认证 方法1
myWebClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
////设定windows网络安全认证 方法2
//NetworkCredential cred = new NetworkCredential("UserName", "UserPWD");
//CredentialCache cache = new CredentialCache();
//cache.Add(new Uri("UploadPath"), "Basic", cred);
//myWebClient.Credentials = cache;
//要上传的文件
System.IO.FileStream fs = new Syste