2017-03-01 24 views
0

我想將.csv文件上傳到我的應用程序中。該代碼正在我的本地電腦上正常工作。但是當我在我的UAT服務器上部署相同的代碼時,它給了我一個與jQuery相關的錯誤。使用ajax調用控制器mvc上傳csv文件c#不能在uat服務器上工作

誤差詳情如下,

  • POST https://www.dummy.com/Customers/UploadCustomersLead 404(未 實測值)
  • 發送@ jquery的-1.11.1.min.js:4
  • AJAX @ jquery的1.11。 1.min.js:4
  • (匿名)@客戶:592
  • 調度@ jquery的-1.11.1.min.js:3
  • r.handle @ jQuery的1.11.1.min.js: 3

我的控制器代碼如下,

[HttpPost] 
public JsonResult UploadCustomersLead() 
{ 
    string sMessage = ""; 
    bool bStatus = true; 
    bool bError = false; 

    System.IO.StreamWriter file = new System.IO.StreamWriter(System.Configuration.ConfigurationManager.AppSettings["LogFile"].ToString()); 
    file.WriteLine("inside upload cust"); 

    var attachedFile = System.Web.HttpContext.Current.Request.Files["CsvDoc"]; 
    if (attachedFile != null && attachedFile.ContentLength > 0) 
    { 
     string fileName = Path.GetFileName(attachedFile.FileName); 
     string path = Path.Combine(Server.MapPath(ConfigManager.GetUploadCSVFilePathForUpload), fileName); 

     file.WriteLine("inside upload cust path:" + path); 
     try 
     { 
      attachedFile.SaveAs(path); 
      DataTable dt = ConvertCSVToDataTable(path); //DataTable dt = CommonFunction.ConvertCSVToDataTable(path); //Are not working because this method is in PNRMSystem.BLL/CommonBusinessLogic.cs file. 

      List<string> l = new List<string>(); 
      if (dt.Rows.Count > 0) 
      { 
       MerchantMasterService.MerchantMasterClient merchantMasterClient = new MerchantMasterService.MerchantMasterClient(); 
       foreach (DataRow row in dt.Rows) 
       { 
        MerchantMasterService.AddCustomerLeadsFromAdminRequest addCustomerLeadsFromMerchantDashboardRequest = new MerchantMasterService.AddCustomerLeadsFromAdminRequest 
        { 
         RegisteredMobileNumber = row["SellerNumber"].ToString(),   //MerchantMobileNumber; 
         CustomerMobileNumber = row["CustomerNumber"].ToString(),   //CustomerMobileNumber, 
         CustomerName = row["CustomerName"].ToString(),     //CustomerName, 
         Address = row["Address"].ToString(),        //Address, 
         BillAmount = Convert.ToDecimal(row["BillAmount"].ToString())  //BillAmount 
        }; 
                                                     MerchantMasterService.AddCustomerLeadsFromAdminResponse addCustomerLeadsFromMerchantDashboardResponse = merchantMasterClient.AddCustomerLeadsFromAdmin(addCustomerLeadsFromMerchantDashboardRequest); 
        if (addCustomerLeadsFromMerchantDashboardResponse.StatusCode.ToString().ToLower().Equals("failure")) 
        { 
         l.Add(row["CustomerNumber"].ToString()); 
        } 
       } 
      } 
      dt.Dispose(); 
      ModelState.Clear(); 
      var Number = l.Count > 0 ? string.Join(",", l) : "None"; 
      sMessage = "Total Records : " + dt.Rows.Count + "</br>" + "Total Success : " + (dt.Rows.Count - l.Count) + "<br/>" + "Total failure : " + l.Count + "<br/>" + "Total failure Number : " + Number; 
     } 
     catch (Exception ex) 
     { 
      ModelState.AddModelError("File", ex.Message.ToString()); 
      CreateLog.Error(this.GetType(), "Error occured on UploadCustomersLead action in Customers controller in merchant dashboard.", ex); 
      bError = true; 
      sMessage = "An error occured. Please try again."; 
      throw; 
     } 
     finally 
     { 
      if (System.IO.File.Exists(path)) 
      { 
       System.IO.File.Delete(path); 
      } 
      file.Close(); 
     } 
    } 
    else 
    { 
     ModelState.AddModelError("File", "Please Select Your file"); 
    } 
    return Json(new { Message = sMessage, Status = bStatus, Error = bError }, JsonRequestBehavior.AllowGet); 
} 

和.cshtml文件jQuery代碼是爲遵循,

$("#btnUploadCustLeads").click(function() { 
       var fileExtension = ['csv']; 
       if ($("#IDofTheUploader").val() == "" || $("#IDofTheUploader").val() == null || $.inArray($("#IDofTheUploader").val().split('.').pop().toLowerCase(), fileExtension) == -1) { 
        $('#statusBoxUploadCustLeads').removeClass('alert-danger').addClass('alert-success').show(500, function() { 
         $('#statusMessageUploadCustLeads').html("Kindly select the .csv file to be uploaded."); 
        }); 
       } 
       else { 
        $('#imgLoadingAddCustomer').removeAttr("style"); 
        var fileUpload = document.getElementById("IDofTheUploader"); 
        if (fileUpload.value != null) { 
         var uploadFile = new FormData(); 
         var files = $("#IDofTheUploader").get(0).files; 
         // Add the uploaded file content to the form data collection 
         if (files.length > 0) { 
          uploadFile.append("CsvDoc", files[0]); 
          $.ajax({ 
           url: "/Customers/UploadCustomersLead", 
           contentType: false, 
           processData: false, 
           data: uploadFile, 
           type: 'POST', 
           success: function (data) { 
            $('#statusBoxUploadCustLeads').removeClass('alert-danger').addClass('alert-success').show(500, function() { 
             $('#statusMessageUploadCustLeads').html(data.Message); 
             ClearUploadCustLeadsControls(); 
            }); 
           }, 
           complete: function() { 
            $('#imgLoadingAddCustomer').css("display", "none"); 
           } 
          }); 
         } 
        } 
        else { 
         $('#statusBoxUploadCustLeads').removeClass('alert-danger').addClass('alert-success').show(500, function() { 
          $('#statusMessageUploadCustLeads').html("File to be uploaded can not be bank."); 
         }); 
        } 
       } 
      }); 
+0

url:「/ Customers/UploadCustomersLead」,請勿使用絕對網址。 「〜/客戶/ UploadCustomersLead」 –

回答

0

我會發表評論,但我有足夠的代表。

嘗試檢查UAT服務器的路由配置。我有,當我有一個控制器上多個GET方法類似的錯誤 - 我需要指定了方法名作爲參數,使用像這樣的路線:

routes.MapHttpRoute(
      name: "CallMethodByName", 
      routeTemplate: "api/{controller}/{action}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     ); 

如果這一塊配置的是不是在UAT服務器將無法正確處理請求並返回404錯誤。

編輯:新的配置應該在較舊的配置下面(更具體的說明應該晚點)。這應該適用於後期以及獲取方法。你的測試配置與UAT配置相比如何?與

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

routes.MapHttpRoute(
name: "CallMethodByName", 
routeTemplate: "api/{controller}/{action}/{id}", 
defaults: new { id = RouteParameter.Optional } 
); 

routes.MapRoute(
name: "Default", 
url: "{controller}/{action}/{id}", 
defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional } 
); 

在服務器routeconfig.cs文件,但沒有工作,我的操作方法

0

@princeofmince,我已經試過路由配置是[httppost]方法不[HTTPGET]

相關問題