Как отправить файл в формате csv через xhrPost()?

В настоящее время мой элемент формы выглядит следующим образом:

<form enctype="multipart/form-data" name="copyReplaceForm" method="POST" action="/app/applications/copyreplace/postCsv">

Но вместо того, чтобы дать action, enctype а также method на <form>Я хочу отправить его с помощью dojo.xhrPost(),

Может кто-нибудь, пожалуйста, скажите мне, как отправить с помощью xhrPost?

Кроме того, мой кусок кода REST выглядит следующим образом:

@POST
@Path("/bulkCopyReplaceFirst")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.MULTIPART_FORM_DATA)

Мой xhrPost выглядит ниже

var result;
dojo.xhrPost({
        url :"/CopyReplace/bulkCopyR",
        preventCache: true,
        contentType : "multipart/form-data",
        load: function(response) {
                txtResponse = response;
                console.log("response is : txtResponse"+txtResponse)
        },
        error: function(error, ioArgs) {
                console.log("postImageOptions() ERROR :: " + error);
                console.log("postImageOptions() ioArgs :: " + ioArgs);
                return error;
        }


    });
}

2 ответа

URL в xhrPost и путь, указанный в аннотации @Path, не совпадают.

Вы должны добавить form свойство к xhrPost.

var result;
dojo.xhrPost({
        url :"/bulkCopyReplaceFirst",
        form: document.forms["copyReplaceForm"],
        load: function(response) {
                txtResponse = response;
                console.log("response is : txtResponse"+txtResponse)
        },
        error: function(error, ioArgs) {
                console.log("postImageOptions() ERROR :: " + error);
                console.log("postImageOptions() ioArgs :: " + ioArgs);
                return error;
        }


    });
}

Вы можете напрямую использовать Dojo Uploader.

var up = new Uploader({
            label: "Upload csv",
            multiple: false,       // true if you can upload more files
            uploadOnSelect: false,   // true if you want to upload without clicking on the submit of the from
            url: "/path/name.csv",   // the route path to the backend (xhr url)
            style: "",
            onBegin: function() {
               // start of upload
            },
            onProgress: function(rev) {
               // uploading...
            },
            onChange: function() {
               // on file change
               var file = up.getFileList();
             }
         }, this.domNode);
Другие вопросы по тегам