1

I can't understand why this code doesn't work with PUT method and only works with POST

$.ajax({
    xhr: function(){
    var xhr = new window.XMLHttpRequest();
    xhr.upload.addEventListener("progress", function(evt){
        uploadProgress(evt);
    }, false);
    xhr.addEventListener("progress", function(evt){
      console.log('down');
    }, false);
    return xhr;
    },
    url : 'upload',
    type: 'PUT',
    data: data,
    cache: false,
    dataType: 'json',
    processData: false,
    contentType: false,
    success: function(data, textStatus, jqXHR){
        console.log(textStatus);
    },
    error: function(jqXHR, exception){
        alert("error --'");
    }
});

When on type I have POST, uploading works, but it's doesn't works with PUT :X

And there is a way to upload file via PUT without submitting a form?

Because code like that doesn't works:

$('#file-input').on('change', prepareUpload);

var files;

function prepareUpload(event){
    files = event.target.files;
    $.each(files, function(key, value){
        data.append(key, value);
    });

    $.put(
        $(this).parent().attr('action'),
        data,
        onAjaxSuccess_s
    )
}
Ivan
  • 514
  • 5
  • 21
  • That depends on how your sever is setup up. But a `PUT` with the url `/upload` does not make much sense. The url used with the `PUT` request is the url where the resource should be saved. So for a `jpg` a put would look like `/uploads/test.jpg`. ([PUT vs POST in REST](https://stackoverflow.com/questions/630453/put-vs-post-in-rest)) – t.niese Mar 19 '15 at 23:04
  • Hello @t.niese! I think I have a good configuration of my nodeJS server, and on all examples that I can find I always see that type of url. All information is on a request variable. But I will analyze your suggestion. – Ivan Mar 19 '15 at 23:11
  • Without knowing your server code it is not possible to tell you why the `PUT` method is not accepted. The Specs are clear about the difference between `POST` and `PUT` `[...]The URI in a POST request identifies the resource that will handle the enclosed entity. [...]In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource.[...]`. So `PUT upload` is wrong. – t.niese Mar 19 '15 at 23:20
  • @t.niese Hello! I tested your suggestion, and 'test.png' is only a string, doesn't have any parameters `router.param('param1', function(req, res, next, param1){ console.log(param1); //return 'test.png' req.param1 = param1; next(); });` – Ivan Mar 20 '15 at 12:13
  • @t.niese I need to receive something like that: ( console.log(req.files); ) `{ files: { fieldName: 'files', originalFilename: 'web.png', path: 'C:\\Users\\Ivan\\AppData\\Local\\Temp\\6264-1a629s8.png', headers: { 'content-disposition': 'form-data; name="files"; filename="web.png"', 'content-type': 'image/png' }, ws: [ .too long code .], size: 13998, name: 'web.png', type: 'image/png' } }` – Ivan Mar 20 '15 at 12:36

0 Answers0