利用JQuery上传插件Uploadify实现图片上传MongoDB并预览

#region 通过js对 基本的file控件进行封装,实现报名人照片上传<span style="white-space:pre"></span><pre name="code" class="csharp">public static string AppleyPhotoName = ""; //定义一个全局变量,存放报名人照片的名称public static string Name = ""; //定义一个全局变量,存放委托书的照片public ActionResult UpApplyPicture(HttpPostedFileBase Filedata){#region 对文件信息的完整性进行检验// 如果没有上传文件||上传文件的文件名为空||上传文件的文件内容为空if (Filedata == null ||string.IsNullOrEmpty(Filedata.FileName) ||Filedata.ContentLength == 0){return this.HttpNotFound();}#endregion#region 将上传文档暂时存到服务器上,方便对文件创建索引//获得文件名称string strFileName = System.IO.Path.GetFileName(Filedata.FileName);//上传文件暂时存放的相对(虚拟)路径string virtualPath =string.Format("../Content/{0}", strFileName);//上传文件暂时存放的绝对路径//var strPath = this.Server.MapPath(virtualPath);var strPath = this.Server.MapPath(virtualPath);AppleyPhotoName = Filedata.FileName;//将文件保存到该路径下Filedata.SaveAs(strPath);//Applicants();//return View();//将图片保存在mongoDB数据库中UpLoad(strPath, AppleyPhotoName);// #region 关闭数据库连接,删除临时文件//MongoHelper mgHelper = new MongoHelper();//mgHelper.CloseMongoDB();////判断文件是不是存在//if (System.IO.File.Exists(strPath))//{// //如果存在则删除// System.IO.File.Delete(strPath);//}// #endregionreturn this.Json(new { });#endregion}#endregion#region 通过js对 基本的file控件进行封装,实现委托书上传public ActionResult file_upload(HttpPostedFileBase Filedata){#region 对文件信息的完整性进行检验// 如果没有上传文件||上传文件的文件名为空||上传文件的文件内容为空if (Filedata == null ||string.IsNullOrEmpty(Filedata.FileName) ||Filedata.ContentLength == 0){return this.HttpNotFound();}#endregion#region 将上传文档暂时存到服务器上,方便对文件创建索引//获得文件名称string strFileName = System.IO.Path.GetFileName(Filedata.FileName);//上传文件暂时存放的相对(虚拟)路径string virtualPath =string.Format("../Content/{0}", strFileName);//上传文件暂时存放的绝对路径//var strPath = this.Server.MapPath(virtualPath);var strPath = this.Server.MapPath(virtualPath);Name = Filedata.FileName;//将文件保存到该路径下Filedata.SaveAs(strPath);//Applicants();//return View();//将图片保存在mongoDB数据库中UpLoad(strPath,Filedata.FileName);// #region 关闭数据库连接,删除临时文件//MongoHelper mgHelper = new MongoHelper();//mgHelper.CloseMongoDB();////判断文件是不是存在//if (System.IO.File.Exists(strPath))//{// //如果存在则删除// System.IO.File.Delete(strPath);//}// #endregionreturn this.Json(new { });#endregion}#endregion#region 将图片保存在mongoDB数据库中 郑浩 2015年7月11日public string UpLoad(string fileName, string Photoname){MongoHelper mgHelper = new MongoHelper();Document doc = new Document();FileStream fs = null;fs = new FileStream(fileName, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite);Bitmap bt = new Bitmap(fs);int streamLength = (int)fs.Length;byte[] image = new byte[streamLength];fs.Read(image, 0, streamLength);doc["ID"] = Guid.NewGuid();doc["Img"] = image;doc["Name"] = Photoname;mgHelper.MongoCollection.Save(doc);return "图片保存呢成功";}#endregion#region 获取委托书照片的名称/// <summary>/// 获取照片的名称/// </summary>/// <returns></returns>public string PhotoName(){//Response.Write(strPath1);return Name;////#region 关闭数据库连接,删除临时文件//MongoHelper mgHelper = new MongoHelper();//mgHelper.CloseMongoDB();////判断文件是不是存在//if (System.IO.File.Exists(strPath1))//{// //如果存在则删除// System.IO.File.Delete(strPath1);//}//#endregion}#endregion#region 获取报名人照片的名称/// <summary>/// 获取照片的名称/// </summary>/// <returns></returns>public string AppleyPhoto(){//Response.Write(strPath1);return AppleyPhotoName;//#region 关闭数据库连接,删除临时文件//MongoHelper mgHelper = new MongoHelper();//mgHelper.CloseMongoDB();////判断文件是不是存在//if (System.IO.File.Exists(strPath1))//{// //如果存在则删除// System.IO.File.Delete(strPath1);//}//#endregion}#endregion

通过上面这三个部分的代码我们就解决了这个问题,基本上实现了这个需求,,但是自己

人若软弱就是自己最大的敌人

利用JQuery上传插件Uploadify实现图片上传MongoDB并预览

相关文章:

你感兴趣的文章:

标签云: