0

I'm using this plugin: https://github.com/blueimp/jQuery-File-Upload/wiki, the problem is when I click upload and give the button to save it calls a controller, so far so good but when he gives the click the button needs to come some other attributes and I'm not getting to bring these attributes along with the file. Follows the code: /View

 $("#anexoUpload").fileupload({
                dataType: 'json',
                add: function (e, data) {
                    data.context = $('#salvarAnexo')
                      .on('click', function () {
                          data.submit();
                          $.ajax({
                                  url: "@Url.Action("AdicionarAnexo")",
                                  type: "POST",
                                  cache: false
                              }).done(function (data) {
                              });
                            data.context = $('<p/>').text('Uploading...').appendTo(document.body);
                            data.submit();
                        });
                },
                done: function (e, data) {
                    alert('Arquivo salvo com sucesso!');
                    $(".bar").attr("style", "width:0%;")
                }

<li id="anexo">
                                <span>Anexo</span>
                                <input type="file" name="files[]" id="anexoUpload" />
                                <div id="progress">
                                    <button id="salvarAnexo">Salvar Anexo</button>
                                    <div class="bar" style="width: 0%;"></div>
                                </div>

                            </li>
                });

The problem that I believe is in the calling function inside the click fileupload. I'm using this plugin to not need to postback on the page and enter yes only, and when you insert also take two id's to be used for insertion into the database and return the ajax to display a list of attachments.

//Controller

   [HttpPost]
        public ActionResult AdicionarAnexo()
        {
            var lista = new List<UploadFilesResult>();
            foreach (string file in Request.Files)
            {
                HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
            }
         }

Thanks

1 Answer 1

0

you can pass data through an ajax call using the data attribute

$.ajax({
    url: "@Url.Action("AdicionarAnexo")",
    type: "POST",
    data: { id1: "id1", id2: "id2" },
    cache: false

})

then on your controller

public ActionResult AdicionarAnexo(string id1, string id2)

you can name them what you want, just make sure that the name on the view exactly matches the name on the controller

Sign up to request clarification or add additional context in comments.

5 Comments

but in this case if i put the id's he receives the document, this is the problem.
I am not understanding. I thought you needed to pass additional information to save to the database. My answer will allow you to pass more. You said if you send more information he gets the document and you don't want this? What are you needing?
if in the ajax call i pass the ids in my controller what i receive? the file not uploaded
from the code that you posted, the file isn't being passed through the ajax call. The ajax call, calls a method on the controller and Request.Files pulls the file from the view. here is a link where they discuss issues with the request.files stackoverflow.com/questions/4232347/…
That's sound is right, but i need this without postback. Yes the file isn't passed through the ajax call, and when i call this ajax em pass in data the ids, the Request.Files not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.