情况是这样的,写了一个js的上传功能,本地上运行良好,但是代码放到服务器上以后,就出现:未发现‘D:\a.doc’的一部分问题。
问题我已经知道在哪儿了,但是一时找不到解决的办法。请各位大侠点解一二。
JS:
- JScript code
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")(""); }, error: function(data, status, e) { alert("上传失败:" + e.toString()); //$("#btnSave").removeAttr("disabled"); //return false; } }); //$("#btnSave").removeAttr("disabled"); return (result == 0) ? false : true; }
C# 省略了一点
- C# code
//获取要保存的文件信息 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 System.IO.FileStream(fileNamePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); //FileStream fs = OpenFile(); System.IO.BinaryReader r = new System.IO.BinaryReader(fs); //使用UploadFile方法可以用下面的格式 //myWebClient.UploadFile(toFile, "PUT",fileNamePath); byte[] postArray = r.ReadBytes((int)fs.Length); System.IO.Stream postStream = myWebClient.OpenWrite(toFile, "PUT"); if (postStream.CanWrite) { postStream.Write(postArray, 0, postArray.Length); } else { return "0|" + serverFileName + "|" + "文件目前不可写!"; } postStream.Close();