public HttpResponseMessage GetFileMetaData(string fileName) { try { string filePath = string.Concat(this.GetDownloadPath(), "\\", fileName); FileInfo fileInfo = new FileInfo(filePath); FileMetaData metaData = new FileMetaData(); if (!fileInfo.Exists) { metaData.FileResponseMessage.IsExists = false; metaData.FileResponseMessage.Content = string.Format("{0} file is not found !", fileName); return Request.CreateResponse(HttpStatusCode.NotFound, metaData, new MediaTypeHeaderValue("text/json")); } metaData.FileResponseMessage.IsExists = true; metaData.FileName = fileName; metaData.FileExtension = fileInfo.Extension; metaData.FileSize = fileInfo.Length; metaData.FilePath = filePath; metaData.FileResponseMessage.Content = string.Format("{0} file is found !", fileName); return Request.CreateResponse(HttpStatusCode.OK, metaData, new MediaTypeHeaderValue("text/json")); } catch (Exception exception) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception); } }
public HttpResponseMessage SearchFileInDownloadDirectory(string fileName) { List<FileMetaData> filesMetaData = new List<FileMetaData>(); FileMetaData metaData = new FileMetaData(); try { string[] files = Directory.GetFiles(this.GetDownloadPath(), fileName, SearchOption.AllDirectories); if (files != null && files.Count() > 0) { foreach (string file in files) { FileInfo fileInfo = new FileInfo(file); metaData = new FileMetaData(); metaData.FileResponseMessage.IsExists = true; metaData.FileName = fileName; metaData.FileExtension = fileInfo.Extension; metaData.FileSize = fileInfo.Length; metaData.FilePath = file.Replace(fileInfo.Name, ""); filesMetaData.Add(metaData); } return Request.CreateResponse(HttpStatusCode.OK, filesMetaData, new MediaTypeHeaderValue("text/json")); } filesMetaData.Add(new FileMetaData() { FileResponseMessage = new FileResponseMessage { IsExists = false, Content = string.Format("{0} file is not found !", fileName) } }); return Request.CreateResponse(HttpStatusCode.NotFound, filesMetaData, new MediaTypeHeaderValue("text/json")); } catch (Exception exception) { // Log exception and return gracefully metaData = new FileMetaData(); metaData.FileResponseMessage.Content = ProcessException(exception); filesMetaData.Add(metaData); return Request.CreateResponse(HttpStatusCode.InternalServerError, filesMetaData, new MediaTypeHeaderValue("text/json")); } }
public HttpResponseMessage DownloadFile(string fileName) { HttpResponseMessage response = Request.CreateResponse(); try { string filePath = string.Concat(this.GetDownloadPath(), "\\", fileName); FileInfo fileInfo = new FileInfo(filePath); FileMetaData metaData = new FileMetaData(); if (!fileInfo.Exists) { metaData.FileResponseMessage.IsExists = false; metaData.FileResponseMessage.Content = string.Format("{0} file is not found !", fileName); response = Request.CreateResponse(HttpStatusCode.NotFound, metaData, new MediaTypeHeaderValue("text/json")); } else { response.Headers.AcceptRanges.Add("bytes"); response.StatusCode = HttpStatusCode.OK; response.Content = new StreamContent(fileInfo.ReadStream()) { Headers = {ContentType = new MediaTypeHeaderValue("application/octet-stream")} }; response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; response.Content.Headers.ContentLength = fileInfo.Length; } } catch (Exception exception) { response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception); } return response; }