主要步骤如下: 1,在asp.net mvc项目中引入jqrte类库,声明辅助类用于存储服务器端上载文件的信息
复制代码 代码如下: public class ViewDataUploadFilesResult { public string message { get; set; } //public int Length { get; set; } public string imagepath { get; set; } public string error { get; set; } }
2,编写处理文件上载服务器段代码,并将上载的文件信息返回给客户端,代码如下:
复制代码 代码如下: [AcceptVerbs(HttpVerbs.Post)] public JsonResult UploadFiles(FormCollection collection) { var r = new ViewDataUploadFilesResult(); foreach (string file in Request.Files) { string url = Request.Url.Authority; url = "http://" + url; HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase; string date = DateTime.Now.Date.ToShortDateString(); string path = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "Content"); string datePath = Path.Combine(path,date); Directory.CreateDirectory(datePath); url += "/Content/"; url += date; url += "http://www.jb51.net/"; url += Path.GetFileName(hpf.FileName); if (hpf.ContentLength == 0) continue; string savedFileName = Path.Combine( datePath, Path.GetFileName(hpf.FileName)); try { hpf.SaveAs(savedFileName); } catch (Exception e) { r.error = e.ToString(); } //r.Name = savedFileName; //r.Length = hpf.ContentLength; r.imagepath = url; r.message = "ok"; r.error = "ok"; //r.Add(new ViewDataUploadFilesResult() //{ // Name = savedFileName, // Length = hpf.ContentLength /
(编辑:成都站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|