C#文件上傳


 //var fileName = System.IO.Path.GetFileName(upload.FileName);
            //var filePhysicalPath = Server.MapPath("~/Content/" + fileName);//我把它保存在網站根目錄的 upload 文件夾
            var fileName = Path.Combine(Request.MapPath("~/Content"),
               Path.GetFileName(upload.FileName));
            upload.SaveAs(fileName);

            var url = "/Content/" + upload.FileName;
            var CKEditorFuncNum = System.Web.HttpContext.Current.Request["CKEditorFuncNum"];

            //上傳成功后,我們還需要通過以下的一個腳本把圖片返回到第一個tab選項
            return Content("<script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + url + "\");</script>");
      

 

 string fileExt = postFile.FileName.Substring(postFile.FileName.LastIndexOf(".") + 1); //文件擴展名,不含“.”
                    string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "." + fileExt; //隨機生成新的文件名
                    string upLoadPath = "~/Picture/"; //上傳目錄相對路徑
                    string fullUpLoadPath = Server.MapPath(upLoadPath);  //將路徑轉換成 物理路徑
                    string newFilePath = upLoadPath + newFileName; //上傳后的路徑
                    postFile.SaveAs(fullUpLoadPath + newFileName);  //核心方法
                    return Json(new { success=true, message="", remark= "/Picture/" + newFileName });


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM