コード例 #1
0
        public JsonResult SaveAndUploadDataJS()
        {
            var ajaxResponse = new AjaxResponse {
                Success = false, Message = "An error occurred while saving the upload."
            };

            //added additional try/catches for debugging a problem

            try
            {
                //should have already been caught by client, but check again
                if (!ModelState.IsValid)
                {
                    ajaxResponse.Message = "Please complete all required form fields.";
                    return(Json(ajaxResponse));
                }
            }
            catch (Exception ex)
            {
                ErrorTools.HandleError(ex, ErrorLevel.NonFatal);    //just log, no redirect
                ajaxResponse.Message = "There was a problem validating your input, please try again.";
                return(Json(ajaxResponse));
            }

            //validation
            HttpPostedFileBase hpf;

            try
            {
                if (Request.Files.Count != 1)
                {
                    ajaxResponse.Message = "More than one upload file was specified.";
                    return(Json(ajaxResponse));
                }

                hpf = Request.Files[0] as HttpPostedFileBase;
                if (hpf.ContentLength == 0)
                {
                    ajaxResponse.Message = "The specified upload file is empty.";
                    return(Json(ajaxResponse));
                }

                int maxFileBytes = Properties.Settings.Default.MaxUploadSize;
                if (hpf.ContentLength > maxFileBytes)
                {
                    ajaxResponse.Message = string.Format("The upload file should not exceed {0} bytes.", maxFileBytes);
                    return(Json(ajaxResponse));
                }

                if (!Request.Files[0].FileName.ToLower().EndsWith(".csv"))
                {
                    ajaxResponse.Message = "The upload file must be a CSV file.";
                    return(Json(ajaxResponse));
                }
            }
            catch (Exception ex)
            {
                ErrorTools.HandleError(ex, ErrorLevel.NonFatal);    //just log, no redirect
                ajaxResponse.Message = "There was a problem validating the specified file.";
                return(Json(ajaxResponse));
            }

            //int extractId = Convert.ToInt32(Request.Form["extractIdJS"]);
            string           extractName;
            string           internalUserId;
            ExtractViewModel extract;

            ajaxResponse.Success = false;

            try
            {
                extractName    = Request.Form["extractNameJS"];
                internalUserId = Utility.GetAspNetUserName(this);
                extract        = ExtractViewModel.NewExtract(_unitOfWork, extractName, internalUserId);

                if (!extract.IsUniqueYN())   //Uniqueness of extract
                {
                    ajaxResponse.Message = "The upload name already exists.";
                    return(Json(ajaxResponse));
                }
            }
            catch (Exception ex)
            {
                ErrorTools.HandleError(ex, ErrorLevel.NonFatal);    //just log, no redirect
                ajaxResponse.Message = "There was a problem uniqueness of upload file.";
                return(Json(ajaxResponse));
            }

            try
            {
                extract.ExtractId = extract.AddAndSave();

                if (extract.ExtractId == 0)
                {
                    return(Json(ajaxResponse));
                }

                ajaxResponse.Id = extract.ExtractId;
            }
            catch (Exception ex)
            {
                ErrorTools.HandleError(ex, ErrorLevel.NonFatal);    //just log, no redirect
                ajaxResponse.Message = "There was a problem saving the main upload record.";
                return(Json(ajaxResponse));
            }

            //proceed with upload
            try
            {
                extract.UploadExtract(hpf.InputStream);

                ajaxResponse.Message = "Success";
                ajaxResponse.Success = true;
            }
            catch (Exception ex)
            {
                ErrorTools.HandleError(ex, ErrorLevel.NonFatal);    //just log, no redirect
                ajaxResponse.Message = "An error occurred while uploading the data.";
            }

            return(Json(ajaxResponse));
        }