Pass the file and line in one AJAX request



  • Shipping function:

     function send(xml) {  //отправка данных на сервер    
                    var file_data = $('#sortpicture').prop('files')[0];
                    var form_data = new FormData();
                    form_data.append('file', file_data);
                    $.ajax({
                        type: 'POST',
                        url: '/Home/Form',  
                        data: { xml: xml},form_data,
                        success: function (responce) {
                            SaveToLocal("s",xml);// префикс s - sended
                            notification("Данные успешно отправлены", "success");
                        },
                    error: function (xhr, str) {
                            SaveToLocal("u", xml);// префикс u - unsended
                            notification("Подключение недоступно, данные сохранены в локальном хранилище", "warning");
                            setTimeout(DelayResend, 30 * 1000);// функция отложенной отпраки с тайм аутом 30 сек.
                        }
                    })
    
        }; - строка передается в параметре, а файл - из формы.
    

    Controller, which I'm sending:

     [HttpPost]
    [ValidateInput(false)]
    public void Form(string xml)

    {
    
        if (Request.Files.Count != 0)
        {
            string fileName = "";
            if (Request.Browser.Browser.Contains("InternetExplorer"))
                fileName = System.IO.Path.GetFileName(Request.Files[0].FileName);
            else
                fileName = Request.Files[0].FileName;
    
            string filePath = string.Format("~/images/{0}", fileName);
            Request.Files[0].SaveAs(Server.MapPath(filePath));
        }
    
        string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        using (StreamWriter outputFile = new StreamWriter( @"\WriteLines.txt",true))
        outputFile.WriteLine(xml);
    }
    

    The line is moving, there's no file. And so did the edac. They'll be willing to pass either the file or the line.



  • Add the line in form-data:

    form_data.append('xml', xml);
    

    If, after that, it did not fall within the xml of the controller, it could always be obtained:

    string xml = null;
    foreach (var key in Request.Form.AllKeys)
    {
        if (key == "xml")
            xml = Request.Form.Get(key);
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2