Tuesday, 26 January 2016

How to upload file and another data to use the form and work? How to use loader?

There are two css and jquery-alert
_____________________

<link href="@Url.Content("~/Content/jQueryAlert/jquery.alerts.css")" rel="stylesheet" />
<script src="@Url.Content("~/Content/jQueryAlert/jquery.alerts.js")"></script>

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<form enctype="multipart/form-data">
    <div id="status" style=" color: green;"></div>

    <div class="col-sm-5">
        <div class="form-group">
            <label>Weekend Date:<span id="errorWeekendDate">*</span></label>
            @Html.TextBoxFor(model => model.WeekendDate, new { @Placeholder = "Weekend Date", @required = "required", @style = "height: 35px;border: 1px solid #ccc;border-radius: 4px;" })
            <div class="clearfix"></div>
        </div>
    </div>
    <div class="col-sm-7">
        <a class="file-input-wrapper btn btn-default" style="margin-bottom: 15px;">
            <input type="file" name="file" id="fileInput" data-filename-placement="inside" style="left: -225.5px; top: 13px;">
        </a>

        <input type="submit" value="Upload file" style="position: relative;top: -5px;" />
    </div>
    <br />
</form>

<div class="clearfix"></div>
<div id="dvmismatch" style="display :none;">
    @Html.Partial("_MismatchVendorList", Model)
</div>


<script type="text/javascript">
    $(function () {
        // When your form is submitted
        $("form").submit(function (e) {
            debugger;
            var errorWeekendDate = document.getElementById('errorWeekendDate');
            var WeekendDate = document.getElementById('WeekendDate');

            if (WeekendDate.value == '') {
                errorWeekendDate.style.color = "red";
                return false;
            }
            else {
                errorWeekendDate.style.color = "";
            }
            e.preventDefault();
            // Serialize your form
            var formData = new FormData($(this)[0]);
            loader();

            // Make your POST
            $.ajax({
                type: 'POST',
                url: '@Url.Action("FileCsv", "InvoiceProcessing")',
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success: function (result) {
                    debugger;
                    $("#popup_ok").click();
                    document.getElementById('dvmismatch').style.display = 'block';
                    $("#dvmismatch").html(result);
                },
                error: function (result) { alert(''); }

            });
        });
    })
</script>


<script type="text/javascript">
    function loader() {
        debugger;
        jAlert('<img src="@Url.Content("~/Content/images/ajax-loader-large.gif")" height="60" width="60" />', 'Please wait');
        $("#popup_ok").hide();
    }
</script>

 [HttpPost]
        public ActionResult FileCsv(HttpPostedFileBase file, string WeekendDate)
        {
            try
            {
                int result = 0;
                string List = string.Empty;
                InventoryModel model = new InventoryModel();
                InventoryBL objbl = new InventoryBL();
                DataSet ds = new DataSet();
                if (Request.Files["file"].ContentLength > 0)
                {
                    string fileExtension =
                                        System.IO.Path.GetExtension(Request.Files["file"].FileName);

                    string ImportCSVFile = Request.Files["file"].FileName;
                    string FileName = Request.Files["file"].FileName;
                    string[] strFileName = FileName.Split('.');
                    FileName = strFileName[0];

                    string fileLocation = Server.MapPath("~/Content/CSVFile/") + Request.Files["file"].FileName;
                    if (System.IO.File.Exists(fileLocation))
                    {
                        System.IO.File.Delete(fileLocation);
                    }
                    Request.Files["file"].SaveAs(fileLocation);

                    DataTable dt = new DataTable();

                    string fileName1 = Request.Files["file"].FileName;
                    string path = Path.Combine(Server.MapPath("~/Content/CSVFile/"), fileLocation);
          }

                return PartialView("_MismatchVendorList", model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }





















No comments:

Post a Comment