首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

asp.net MVC 文件上传的有关问题

2012-04-10 
asp.net MVC 文件上传的问题或得客户端上传的文件路径,能不能controller里实现文件的上传??C# code public

asp.net MVC 文件上传的问题
或得客户端上传的文件路径,能不能controller里实现文件的上传??


C# code
 public ActionResult UploadFiles(string fileurl){   //fileurl 是要上传的文件的路径,包括文件名   //前台页面没有fileupload控件,也就是说给你一个文件路径,能否上传到服务器?   ?????How To DO?}




[解决办法]
C# code
        /// <summary>        /// 基本设置处理        /// </summary>        /// <returns></returns>        [WebFilter]        [UsersChoolFilter]        [AcceptVerbs(HttpVerbs.Post)]        public ActionResult SchoolConfig(FormCollection form, [color=#FF0000]HttpPostedFileBase NewTrainSchoolLogo[/color], HttpPostedFileBase NewTrainSchoolImg)        {            GetAllModel();            UpdateModel<TrainSchool>(_TSModel);//2009-8-29            _TSModel.TrainSchoolName = Include.StrFilter(_TSModel.TrainSchoolName);            _TSModel.TrainKeyWords = Include.StrFilter(_TSModel.TrainKeyWords);            _TSModel.TrainSchoolCount = Include.StrFilter(_TSModel.TrainSchoolCount);            //////////////////////////////上传图片处理///////////////////////////////////////            [color=#FF0000]string NewLogoStr = CheckImg(NewTrainSchoolLogo,"logo");            string NewImgStr = CheckImg(NewTrainSchoolImg, "Img");            if (NewLogoStr != "") _TSModel.TrainSchoolLogo = NewLogoStr;            if (NewImgStr != "") _TSModel.TrainSchoolImg = NewImgStr;[/color]            //////////////////////////////上传图片处理///////////////////////////////////////            _TSManage.Update(_TSModel);            return Redirect("SchoolConfig");        }        /// <summary>        /// 上传图片处理         /// </summary>        /// <param name="ImgType"></param>        /// <returns></returns>        public string CheckImg(HttpPostedFileBase Files,string NameStr)         {            if (Files == null) return "";            string FileType = Files.FileName.Substring(Files.FileName.LastIndexOf(".") + 1);            if (FileType == "gif" || FileType == "GIF" || FileType == "jpg" || FileType == "JPG" || FileType == "png" || FileType == "PNG")            {                //新的文件名                string ImgName = NameStr + DateTime.Now.ToString("yyyyMMddHHmmssfff")+"."+FileType;                Files.SaveAs(Server.MapPath("/schoolUp/"+ImgName));                return ImgName;            }            else            {                return "";            }        }
[解决办法]
<input type="file" id="fileImage" name="fileImage" />&nbsp;&nbsp;
<input type="submit" value="Upload" />


public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection collection)
{
if (Request.Files.Count == 0)
{
return View();
}
var c = Request.Files[0];
if (c != null && c.ContentLength > 0)
{
int lastSlashIndex = c.FileName.LastIndexOf("\\");
string fileName = c.FileName.Substring(lastSlashIndex + 1, c.FileName.Length - lastSlashIndex - 1);
fileName = Path.Combine(CommonUtility.DocImagePath, fileName);
c.SaveAs(fileName);
}
return View();
}
 MVC 上传

热点排行