public void GeneralUpload(int userId) { Models.ComboResponse _response = new Models.ComboResponse(); _response.bool_result = true; _response.ErrorCode = 0; _response.ErrorMsg = ""; try { if (HttpContext.Current.Request.Files.Count != 0) { var ext = Path.GetExtension(HttpContext.Current.Request.Files[0].FileName); var fileName = Path.GetFileName(HttpContext.Current.Request.Files[0].FileName); if (HttpContext.Current.Request.Files[0].FileName.LastIndexOf("\\") != -1) { fileName = HttpContext.Current.Request.Files[0].FileName.Remove(0, HttpContext.Current.Request.Files[0].FileName.LastIndexOf("\\")).ToLower(); } DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/userfiles/" + userId.ToString())); if (!dir.Exists) dir.Create(); string FilePath = "/userfiles/" + userId.ToString() + fileName + ext; string location = HttpContext.Current.Server.MapPath("~/" + FilePath); HttpContext.Current.Request.Files[0].SaveAs(location); BLL.Attachment newfile = new BLL.Attachment(); newfile.AddNew(); newfile.Path = FilePath; newfile.Save(); Models.Attachment responseText = new Models.Attachment(); responseText.AttachmentID = newfile.AttachmentID; responseText.Path = newfile.Path; _response.Entity = responseText; SetContentResult(_response); } else { _response.bool_result = false; _response.ErrorCode = 21; _response.ErrorMsg = "No file found."; } } catch (Exception ex) { _response.bool_result = false; _response.ErrorCode = 20; _response.ErrorMsg = "Error while uploading file."; } SetContentResult(_response); }
public void ProcessRequest(HttpContext context) { bool isImage = false, isCover = false, isProfile = false; if (context.Request.Form["IsCover"] != null) isCover = Convert.ToBoolean(context.Request.Form["IsCover"].ToString()); if (context.Request.Form["IsProfile"] != null) isProfile = Convert.ToBoolean(context.Request.Form["IsProfile"].ToString()); int userid = Convert.ToInt32(context.Request.Form["UserId"].ToString()); int typeid = Convert.ToInt32(context.Request.Form["Type"].ToString()); // 1 - image , 2- audio , 3 - video isImage = (typeid == 1); var ext = System.IO.Path.GetExtension(context.Request.Files[0].FileName); string fileName = Guid.NewGuid().ToString(); DirectoryInfo dir = new DirectoryInfo(context.Server.MapPath("~/userfiles/" + userid.ToString())); if (!dir.Exists) dir.Create(); string location = context.Server.MapPath("~/userfiles/"+ userid.ToString() + "/") + fileName + ext; string thumblocation = context.Server.MapPath("~/userfiles/" + userid.ToString() + "/thumb_") + fileName + ".jpg"; context.Request.Files[0].SaveAs(location); BLL.Attachment newfile = new BLL.Attachment(); newfile.AddNew(); if (typeid == 3) { (new NReco.VideoConverter.FFMpegConverter()).GetVideoThumbnail(location, thumblocation); newfile.ThumbsPath = "/userfiles/" + userid.ToString() + "/thumb_" + fileName + ".jpg"; } if (typeid == 2) { try { //string wavfile = location.Replace(".amr", ".wav"); string newlocation = context.Server.MapPath("~/userfiles/" + userid.ToString() + "/") + fileName + ".mp3"; (new NReco.VideoConverter.FFMpegConverter()).ConvertMedia(location, newlocation, "mp3"); ext = ".mp3"; } catch (Exception e) { } } newfile.Path = "/userfiles/"+ userid.ToString() + "/" + fileName + ext; newfile.AttachmentTypeID = typeid; newfile.Save(); if (isCover || isProfile) { BLL.ComboUser user = new ComboUser(); user.LoadByPrimaryKey(userid); if (isCover) user.CoverImgID = newfile.AttachmentID; if (isProfile) user.ProfileImgID = newfile.AttachmentID; user.Save(); } Models.Attachment responseText = new Models.Attachment(); responseText.AttachmentID = newfile.AttachmentID; responseText.Path = newfile.Path; if (newfile.AttachmentTypeID == 3) responseText.ThumbsPath = newfile.ThumbsPath; responseText.AttachmentTypeID = newfile.AttachmentTypeID; Models.ComboResponse _response = new Models.ComboResponse(); _response.bool_result = true; _response.ErrorCode = 0; _response.ErrorMsg = ""; _response.Entity = new Models.Attachment[] { responseText }; SetContentResult(_response); }