Пример #1
0
        public async Task <HttpResponseMessage> PostDailyFile(int Id)
        {
            if (Request.Content.IsMimeMultipartContent())
            {
                try
                {
                    // Get Destination Folder Path
                    var root = HttpContext.Current.Server.MapPath("~/Uploads/DailyFiles/Daily-" + Id.ToString() + "/");



                    //get provider
                    CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(root);

                    List <string> files = new List <string>();

                    try
                    {
                        await Request.Content.ReadAsMultipartAsync(provider);

                        //Fetch Normal Data
                        var model = provider.FormData["model"];

                        //Fetch Files Inside
                        foreach (MultipartFileData file in provider.FileData)
                        {
                            files.Add(Path.GetFileName(file.LocalFileName));
                        }

                        //Desirilze Json Data Object
                        var dailyFile = JsonConvert.DeserializeObject <DailyFile>(model);

                        //Insert Creation Date
                        dailyFile.CreatedDate = DateTime.UtcNow;

                        if (!ModelState.IsValid)
                        {
                            return(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed));
                        }
                        //Files Name Counter
                        var newname = CreateFileName(root);

                        if (provider.FileData.Count == 0)
                        {
                            File.Copy(HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output.xls"), root + newname + ".xls", true);
                        }
                        foreach (MultipartFileData file in provider.FileData)
                        {
                            //get Original File Path
                            //   dailyFile.FilePath = file.LocalFileName;

                            // Move file from Original File Path to new Place
                            try
                            {
                                if (file.Headers.ContentDisposition.Size != 0)
                                {
                                    File.Move(file.LocalFileName, root + newname + ".xls");
                                }
                                else
                                {
                                    File.Copy(HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output.xls"), root + newname + ".xls", true);
                                }
                            }
                            catch (Exception ex)
                            {
                                return(Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex.Message));
                            }
                        }

                        InsertIntoDailyFiles(dailyFile, root, newname);


                        return(Request.CreateResponse(HttpStatusCode.OK, files));
                    }
                    catch (Exception ex)
                    {
                        return(Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex.Message));
                    }
                }
                catch (Exception ex)
                {
                    return(Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex.Message));
                }
            }



            return(Request.CreateResponse(HttpStatusCode.OK, "success!"));
        }
        public async Task<HttpResponseMessage> PostDailyFile(int Id)
        {

            if (Request.Content.IsMimeMultipartContent())
            {
                try
                {
                    // Get Destination Folder Path
                    var root = HttpContext.Current.Server.MapPath("~/Uploads/DailyFiles/Daily-" + Id.ToString() + "/");



                    //get provider 
                    CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(root);

                    List<string> files = new List<string>();

                    try
                    {
                        await Request.Content.ReadAsMultipartAsync(provider);

                        //Fetch Normal Data
                        var model = provider.FormData["model"];

                        //Fetch Files Inside
                        foreach (MultipartFileData file in provider.FileData)
                        {
                            files.Add(Path.GetFileName(file.LocalFileName));
                        }

                        //Desirilze Json Data Object 
                        var dailyFile = JsonConvert.DeserializeObject<DailyFile>(model);

                        //Insert Creation Date
                        dailyFile.CreatedDate = DateTime.UtcNow;

                        if (!ModelState.IsValid)
                        {
                            return new HttpResponseMessage(HttpStatusCode.MethodNotAllowed);
                        }
                        //Files Name Counter 
                        var newname = CreateFileName(root);

                        if (provider.FileData.Count == 0)
                        {
                            File.Copy(HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output.xls"), root + newname + ".xls", true);
                        }
                        foreach (MultipartFileData file in provider.FileData)
                        {
                            //get Original File Path 
                            //   dailyFile.FilePath = file.LocalFileName;

                            // Move file from Original File Path to new Place
                            try
                            {
                                if (file.Headers.ContentDisposition.Size != 0)
                                {
                                    File.Move(file.LocalFileName, root + newname + ".xls");
                                }
                                else
                                {
                                    File.Copy(HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output.xls"), root + newname + ".xls", true);
                                }
                            }
                            catch (Exception ex)
                            {

                                return Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex.Message);
                            }


                        }

                        InsertIntoDailyFiles(dailyFile, root, newname);


                        return Request.CreateResponse(HttpStatusCode.OK, files);

                    }
                    catch (Exception ex)
                    {
                        return Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex.Message);

                    }

                }
                catch (Exception ex)
                {

                    return Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex.Message);
                }
            }




            return Request.CreateResponse(HttpStatusCode.OK, "success!");

        }