public HttpResponseMessage GetDocuments(string DO, string FileName)
        {
            DocumentDL          dl       = new DocumentDL();
            string              root     = HttpContext.Current.Server.MapPath($"~/App_Data/Files/{DO}/");
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);

            //Set the File Path.
            //Check whether File exists.
            if (!File.Exists(root))
            {
                //Throw 404 (Not Found) exception if File not found.
                response.StatusCode   = HttpStatusCode.NotFound;
                response.ReasonPhrase = string.Format("File not found: {0} .", FileName);
                throw new HttpResponseException(response);
            }

            //Read the File into a Byte Array.
            byte[] bytes = File.ReadAllBytes(root);

            //Set the Response Content.
            response.Content = new ByteArrayContent(bytes);

            //Set the Response Content Length.
            response.Content.Headers.ContentLength = bytes.LongLength;

            //Set the Content Disposition Header Value and FileName.
            response.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = FileName;

            //Set the File Content Type.
            response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(FileName));
            return(response);
        }
        public HttpResponseMessage GetDocNames(string DO)
        {
            DocumentDL        dl        = new DocumentDL();
            List <DocumentBO> list      = dl.GetSupportingDocumentsForDO(Guid.Parse(DO)).ToList();
            List <string>     FileNames = list.Select(y => y.name).ToList();

            return(Request.CreateResponse(HttpStatusCode.OK, FileNames, Configuration.Formatters.JsonFormatter));
        }
Esempio n. 3
0
        public List <DocumentBO> GetDocNames(string DO)
        {
            DocumentDL        dl   = new DocumentDL();
            List <DocumentBO> list = dl.GetSupportingDocuments(DO).ToList();

            return(list);
            // List<string> FileNames = list.Select(y => y.FileName).ToList();
        }
Esempio n. 4
0
        public HttpResponseMessage GetbyKey(string OrderKey)
        {
            DeliveryOrderBO dorder = new DeliveryOrderBO();
            DocumentDL      dl     = new DocumentDL();

            dorder = doObj.GetDeliveryOrder(OrderKey);
            if (dorder.OrderNo.ToString().Trim() != string.Empty)
            {
                List <DocumentBO> list = dl.GetSupportingDocuments(dorder.OrderNo).ToList();
                dorder.file = list;
            }

            return(Request.CreateResponse(HttpStatusCode.OK, dorder, Configuration.Formatters.JsonFormatter));
        }
        public Task <HttpResponseMessage> Post(string DO, string CreatedBy)
        {
            try
            {
                var fileuploadPath = HttpContext.Current.Server.MapPath("~/App_Data/Files/");
                var httpContext    = HttpContext.Current;

                HttpRequestMessage request = this.Request;
                if (!request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType));
                }

                string root = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Files");
                MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(root);

                var task = request.Content.ReadAsMultipartAsync(provider).
                           ContinueWith(o =>
                {
                    foreach (var file in provider.FileData)
                    {
                        FileInfo finfo        = new FileInfo(file.LocalFileName);
                        DocumentBO documentBO = new DocumentBO
                        {
                            Dockey       = Guid.NewGuid(),
                            DocType      = (DocType)Enum.Parse(typeof(DocType), finfo.Extension),
                            CreatedBy    = new UserInfoRepository().GetbyField(CreatedBy).userkey,
                            FileSizeInMB = (int)finfo.Length / 1024,
                            FileType     = string.Empty, //not sure
                            name         = finfo.Name
                        };
                        OrderHeaderDocumentBO orderBO = new OrderHeaderDocumentBO
                        {
                            Document = documentBO,
                            Orderkey = Guid.Parse(DO)
                        };

                        //renaming the random file to Original file name
                        string uploadingFileName = provider.FileData.Select(x => x.LocalFileName).FirstOrDefault();
                        string originalFileName  = String.Concat(fileuploadPath, "\\" + (provider.Contents[0].Headers.ContentDisposition.FileName).Trim(new Char[] { '"' }));

                        if (File.Exists(originalFileName))
                        {
                            File.Delete(originalFileName);
                        }

                        File.Move(finfo.FullName, Path.Combine(root, DO,
                                                               file.Headers.ContentDisposition.FileName.Replace("\"", "")));
                        DocumentDL dl = new DocumentDL();
                        dl.InsertDOHeaderDocument(orderBO);
                    }
                    return(new HttpResponseMessage()
                    {
                        Content = new StringContent("File uploaded.")
                    });
                }
                                        );
                return(task);
            }
            catch
            {
                return(null);
            }
        }
 public DocumentBL()
 {
     _documentDL = new DocumentDL();
 }
Esempio n. 7
0
        public async Task <HttpResponseMessage> Upload()
        {
            try
            {
                var fileuploadPath = HttpContext.Current.Server.MapPath("~/App_Data/Files/");

                var provider = new MultipartFormDataStreamProvider(fileuploadPath);
                var content  = new StreamContent(HttpContext.Current.Request.GetBufferlessInputStream(true));
                foreach (var header in Request.Content.Headers)
                {
                    content.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }

                await content.ReadAsMultipartAsync(provider);

                string DO        = provider.FormData[0];
                string CreatedBy = provider.FormData[1];

                ////renaming the random file to Original file name
                //string uploadingFileName = provider.FileData.Select(x => x.LocalFileName).FirstOrDefault();
                //string originalFileName = String.Concat(fileuploadPath, "\\" + (provider.Contents[0].Headers.ContentDisposition.FileName).Trim(new Char[] { '"' }));

                //if (File.Exists(originalFileName))
                //{
                //    File.Delete(originalFileName);
                //}

                //File.Move(uploadingFileName, originalFileName);

                foreach (var file in provider.FileData)
                {
                    //renaming the random file to Original file name
                    string uploadingFile = file.LocalFileName;
                    string originalFile  = String.Concat(fileuploadPath, DO + "\\" + (file.Headers.ContentDisposition.FileName).Trim(new Char[] { '"' }));
                    string strFileName   = file.Headers.ContentDisposition.FileName.Replace('"', ' ').Trim();
                    if (File.Exists(originalFile))
                    {
                        File.Delete(originalFile);
                    }

                    if (!Directory.Exists(String.Concat(fileuploadPath, DO)))
                    {
                        Directory.CreateDirectory(String.Concat(fileuploadPath, DO));
                    }

                    File.Move(uploadingFile, originalFile);

                    FileInfo   finfo      = new FileInfo(originalFile);
                    DocumentBO documentBO = new DocumentBO()
                    {
                        Dockey       = Guid.NewGuid(),
                        DocType      = (DocType)Enum.Parse(typeof(DocType), finfo.Extension.Replace('.', ' ').Trim().ToUpper()),
                        CreatedBy    = Guid.Parse(CreatedBy),
                        FileSizeInMB = (int)finfo.Length / 1024,
                        FileType     = finfo.Extension.Replace('.', ' ').Trim().ToUpper(), //not sure
                        name         = strFileName
                    };

                    OrderHeaderDocumentBO orderBO = new OrderHeaderDocumentBO
                    {
                        Document = documentBO,
                        OrderNo  = DO
                    };

                    DocumentDL dl = new DocumentDL();
                    dl.InsertDOHeaderDocument(orderBO);
                }
                return(Request.CreateResponse(HttpStatusCode.Created, new StringContent(" Files Uploaded Successfully"), Configuration.Formatters.JsonFormatter));
            }
            catch (Exception ex)
            {
                //return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message, Configuration.Formatters.JsonFormatter);

                return(Request.CreateResponse(HttpStatusCode.InternalServerError, new StringContent("Upload Failed"), Configuration.Formatters.JsonFormatter));
            }
        }