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