RejectedSoftware Forums

Sign up

File upload using AJAX

Hi there,

how to make file upload via jQuery AJAX work? I tried almost everything but I always get a 400 error. There is no example/documention on that topic available. This is my current guess:

var files = e.target.files;
var data = new FormData();
data.append('upload', files[0]);
$.ajax({
    url: 'upload',
    type: 'POST',
    data: data,
    processData: false,
    contentType: 'multipart/form-data'
});


Thank you

Re: File upload using AJAX

On Tue, 24 Mar 2015 15:08:38 GMT, Henning Pohl wrote:

Hi there,

how to make file upload via jQuery AJAX work? I tried almost everything but I always get a 400 error. There is no example/documention on that topic available. This is my current guess:

var files = e.target.files;
var data = new FormData();
data.append('upload', files[0]);
$.ajax({
    url: 'upload',
    type: 'POST',
    data: data,
    processData: false,
    contentType: 'multipart/form-data'
});


Thank you

I haven't tried this before, so I can't say if it will actually work, but this StackOverflow answer suggests to set contentType to false to get the correct boundary string included.

Re: File upload using AJAX

Tried that before, still 400.

Re: File upload using AJAX

Am 24.03.2015 um 18:11 schrieb Henning Pohl:

Tried that before, still 400.

Try to run with --vv to get debug log output, that should indicate
what goes wrong. Alternatively, the returned 400 document should also
contain a more detailed error message.

Re: File upload using AJAX

--vv gave me this:

[00D1905B:00D2B15B dbg] Exception while handling request POST /upload: vibe.http.common.HTTPStatusException@../../.dub/packages/vibe-d-0.7.22/source/vibe/http/server.d(1426): Request size too big
[00D1905B:00D2B15B dbg] ----------------
[00D1905B:00D2B15B dbg] ./chat(bool vibe.http.common.enforceHTTP!(bool).enforceHTTP(bool, vibe.http.status.HTTPStatus, lazy immutable(char)[], immutable(char)[], int)+0x3f) [0x6d999f]
[00D1905B:00D2B15B dbg] ./chat(bool vibe.http.common.enforceBadRequest!(bool).enforceBadRequest(bool, lazy immutable(char)[], immutable(char)[], int)+0x50) [0x6d9930]
[00D1905B:00D2B15B dbg] ./chat(bool vibe.http.server.handleRequest(vibe.core.stream.Stream, vibe.core.net.TCPConnection, vibe.http.server.HTTPServerListener, ref vibe.http.server.HTTPServerSettings, ref bool)+0xb96) [0x7b9b1a]
[00D1905B:00D2B15B dbg] ./chat(void vibe.http.server.handleHTTPConnection(vibe.core.net.TCPConnection, vibe.http.server.HTTPServerListener)+0x19c) [0x7b8e80]
[00D1905B:00D2B15B dbg] ./chat(void vibe.http.server.listenHTTPPlain(vibe.http.server.HTTPServerSettings).doListen(vibe.http.server.HTTPServerSettings, ulong, immutable(char)[])._lambda4(vibe.core.net.TCPConnection)+0x62) [0x7b8882]
[00D1905B:00D2B15B dbg] ./chat(void vibe.core.drivers.libevent2
tcp.ClientTask.execute()+0x4d4) [0x796a94]
[00D1905B:00D2B15B dbg] ./chat(void vibe.core.core.makeTaskFuncInfo!(void delegate()).makeTaskFuncInfo(ref void delegate()).callDelegate(vibe.core.core.TaskFuncInfo*)+0x72) [0x6ffd46]
[00D1905B:00D2B15B dbg] ./chat(void vibe.core.core.CoreTask.run()+0x18a) [0x6fc066]
[00D1905B:00D2B15B dbg] ./chat(void core.thread.Fiber.run()+0x2a) [0x8b26e2]
[00D1905B:00D2B15B dbg] ./chat(fiber_entryPoint+0x61) [0x8b25f5]

And in fact uploading smaller files works. I was trying to upload ~4MB files, but this should still work.

Re: File upload using AJAX

But where is the file then, no "upload" in req.files.

Re: File upload using AJAX

Am 24.03.2015 um 20:03 schrieb Henning Pohl:

--vv gave me this:

[00D1905B:00D2B15B dbg] Exception while handling request POST /upload: vibe.http.common.HTTPStatusException@../../.dub/packages/vibe-d-0.7.22/source/vibe/http/server.d(1426): Request size too big
[00D1905B:00D2B15B dbg] ----------------
[00D1905B:00D2B15B dbg] ./chat(bool vibe.http.common.enforceHTTP!(bool).enforceHTTP(bool, vibe.http.status.HTTPStatus, lazy immutable(char)[], immutable(char)[], int)+0x3f) [0x6d999f]
[00D1905B:00D2B15B dbg] ./chat(bool vibe.http.common.enforceBadRequest!(bool).enforceBadRequest(bool, lazy immutable(char)[], immutable(char)[], int)+0x50) [0x6d9930]
[00D1905B:00D2B15B dbg] ./chat(bool vibe.http.server.handleRequest(vibe.core.stream.Stream, vibe.core.net.TCPConnection, vibe.http.server.HTTPServerListener, ref vibe.http.server.HTTPServerSettings, ref bool)+0xb96) [0x7b9b1a]
[00D1905B:00D2B15B dbg] ./chat(void vibe.http.server.handleHTTPConnection(vibe.core.net.TCPConnection, vibe.http.server.HTTPServerListener)+0x19c) [0x7b8e80]
[00D1905B:00D2B15B dbg] ./chat(void vibe.http.server.listenHTTPPlain(vibe.http.server.HTTPServerSettings).doListen(vibe.http.server.HTTPServerSettings, ulong, immutable(char)[])._lambda4(vibe.core.net.TCPConnection)+0x62) [0x7b8882]
[00D1905B:00D2B15B dbg] ./chat(void vibe.core.drivers.libevent2
tcp.ClientTask.execute()+0x4d4) [0x796a94]
[00D1905B:00D2B15B dbg] ./chat(void vibe.core.core.makeTaskFuncInfo!(void delegate()).makeTaskFuncInfo(ref void delegate()).callDelegate(vibe.core.core.TaskFuncInfo*)+0x72) [0x6ffd46]
[00D1905B:00D2B15B dbg] ./chat(void vibe.core.core.CoreTask.run()+0x18a) [0x6fc066]
[00D1905B:00D2B15B dbg] ./chat(void core.thread.Fiber.run()+0x2a) [0x8b26e2]
[00D1905B:00D2B15B dbg] ./chat(fiber_entryPoint+0x61) [0x8b25f5]

And in fact uploading smaller files works. I was trying to upload ~4MB files, but this should still work.

You can configure the maximum request size using
HTTPServerSettings.maxRequestSize (2MB by default).

But where is the file then, no "upload" in req.files.

Do you get any entry in req.files? You can iterate over them using
foreach.

The upload functionality generally seems to work for me, at least for
the example in examples/uploader.

Re: File upload using AJAX

Okay so now I can upload text files but no binary files:

var files = e.target.files;
var data = new FormData();
data.append('upload', files[0]);
$.ajax({
    url: 'upload',
    type: 'POST',
    data: data,
    conentType: 'multipart/form-data',
    processData: false
});

[B3C49FB1:AFCF17B1 dbg] Exception while handling request POST /upload: Invalid UTF sequence: a6 - Invalid UTF-8 sequence (at index 1)