public IHttpActionResult GetDownload(string id) { IHttpActionResult result = null; ProfileCommon profile = APIhelper.GetProfileCommon(Request); if (!string.IsNullOrEmpty(id)) { web_Document doc = GetWebDocument(id); if (doc != null) { string file = Path.Combine(Util.GetAppSettings("DocRoot") + doc.Path); var fileinfo = new FileInfo(file); try { if (!fileinfo.Exists) { throw new FileNotFoundException(fileinfo.Name); } result = new FileActionResult(file); } catch (Exception ex) { result = ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "File not found")); } } } return(result); }
public HttpResponseMessage Download(string documentID) { Log.LogSubType subType = Log.LogSubType.DocumentIDInvalid; ProfileCommon profile = APIhelper.GetProfileCommon(Request); bool error = true; if (!string.IsNullOrEmpty(documentID)) { web_Document doc = GetWebDocument(documentID); if (doc != null) { string file = Path.Combine(Util.GetAppSettings("DocRoot") + doc.Path); if (File.Exists(file)) { var response = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(file, FileMode.Open); var fileInfo = new FileInfo(file); response.Content = new StreamContent(stream); response.Content.Headers.ContentLength = stream.Length; response.Content.Headers.ContentType = new MediaTypeHeaderValue(Util.GetMimeTypeFromExtension(fileInfo.Extension)); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileInfo.Name }; return(response); } else { subType = Log.LogSubType.DocumentNotFound; Log.WriteLog(Log.LogType.Exception, User.Identity.Name, subType, documentID, file, null); } } else { subType = Log.LogSubType.DocumentRecordMissing; Log.WriteLog(Log.LogType.Exception, profile.UserName, subType, documentID, null, null); } } if (error) { Log.WriteLog(Log.LogType.Exception, profile.UserName, subType, documentID, null, null); } return(new HttpResponseMessage(HttpStatusCode.OK)); }
private web_Document GetWebDocument(string id) { web_Document doc = null; ProfileCommon profile = APIhelper.GetProfileCommon(Request); using (PolicyDataSourceDataContext db = new PolicyDataSourceDataContext()) { db.ObjectTrackingEnabled = false; doc = (from d in db.web_Documents where d.DocumentID == id && (d.CompanyID == profile.CompanyID) select d).FirstOrDefault(); } return(doc); }