亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

急:一個ajax上傳的問題

急:一個ajax上傳的問題

Helenr 2018-12-07 11:52:38
public string UpLoadFile(string fileNamePath, string toFilePath)??? {??????? try??????? {??????????? //獲取要保存的文件信息??????????? FileInfo file = new FileInfo(fileNamePath);??????????? //獲得文件擴展名??????????? string fileNameExt = file.Extension;??????????? if (file.Length > 41943040)??????????? {??????????????? return "0|errorfile|上傳文件不能超過40M";??????????? }??????????? //驗證合法的文件??????????? if (CheckFileExt(fileNameExt))??????????? {??????????????? //生成將要保存的隨機文件名??????????????? string fileName = GetFileName() + fileNameExt;??????????????? //檢查保存的路徑 是否有/結尾??????????????? if (toFilePath.EndsWith("/") == false) toFilePath = toFilePath + "/"; ??????????????? //按日期歸類保存??????????????? string datePath = DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("dd") + "/";??????????????? if (true)??????????????? {??????????????????? toFilePath += datePath;??????????????? } ??????????????? //獲得要保存的文件路徑??????????????? string serverFileName = toFilePath + fileName;??????????????? //物理完整路徑??????????????????? ??????????????? string toFileFullPath = HttpContext.Current.Server.MapPath(toFilePath); ??????????????? //檢查是否有該路徑? 沒有就創建??????????????? if (!Directory.Exists(toFileFullPath))??????????????? {??????????????????? Directory.CreateDirectory(toFileFullPath);??????????????? } ??????????????? //將要保存的完整文件名??????????????? ??????????????? string toFile = toFileFullPath + fileName; ??????????????? ///創建WebClient實例?????? ??????????????? WebClient myWebClient = new WebClient();??????????????? //設定windows網絡安全認證?? 方法1??????????????? myWebClient.Credentials = CredentialCache.DefaultCredentials;??????????????? //設定windows網絡安全認證?? 方法2??????????????? //NetworkCredential cred = new NetworkCredential("SINOPANEL/xiangnan-xiao", "xiaoxiangnan");??????????????? //CredentialCache cache = new CredentialCache();??????????????? //cache.Add(new Uri("UploadPath"), "Basic", cred);??????????????? //myWebClient.Credentials = cache; ??????????????? //要上傳的文件?????? ??????????????? FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);??????????????? //FileStream fs = OpenFile();?????? ??????????????? BinaryReader r = new BinaryReader(fs);??????????????? //使用UploadFile方法可以用下面的格式?????? ??????????????? //myWebClient.UploadFile(toFile, "PUT",fileNamePath);?????? ??????????????? byte[] postArray = r.ReadBytes((int)fs.Length);??????????????? Stream postStream = myWebClient.OpenWrite(toFile, "PUT");??????????????? if (postStream.CanWrite)??????????????? {??????????????????? postStream.Write(postArray, 0, postArray.Length);??????????????? }??????????????? else??????????????? {??????????????????? return "0|" + serverFileName + "|" + "文件目前不可寫";??????????????? }??????????????? postStream.Close(); ??????????????? return "1|" + serverFileName + "|" + "文件上傳成功";??????????? }??????????? else??????????? {??????????????? return "0|errorfile|" + "文件格式非法";??????????? }??????? }??????? catch (Exception e)??????? {??????????? return "0|errorfile|" + "文件上傳失敗,錯誤原因:" + e.Message;??????? }??? } 這是我用的ajax上傳的? 在本地可以?? 但是傳到服務器上就不能上傳了? 提示未能找到文件,煩請各位大蝦解救
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 312 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號