Hi Supun!

Try Making contentType: false,

Regards,
Malith.

On Thu, Apr 23, 2015 at 5:20 PM, Supun Sethunga <sup...@wso2.com> wrote:

> Hi,
>
> Requirement was to let a user upload a data file from a UI (html/jaggery),
> and the backend which handles file uploading is exposed as a REST service.
> Backend service method I wrote looks as follows:
>
>     @POST
>>     @Produces(MediaType.APPLICATION_JSON)
>>     @Consumes(MediaType.MULTIPART_FORM_DATA)
>>     public Response uploadDataset(@Multipart("datasetName") String
>> datasetName,
>>                                   @Multipart("version") String version,
>>                                   @Multipart("description") String
>> description,
>>                                   @Multipart("sourceType") String
>> sourceType,
>>                                   @Multipart("destination") String
>> destination,
>>                                   @Multipart("sourcePath") String
>> sourcePath,
>>                                   @Multipart("dataFormat") String
>> dataFormat,
>>                                   @Multipart("file") InputStream
>> inputStream) {
>>         . . .
>>         . . .
>>     }
>
>
> The InputStream is then written to a file at the server side. This method
> works fine when I call this with CURL using:
>
> *curl -X POST -b cookies  http://localhost:9763/api/datasets
>> <http://localhost:9763/api/datasets> -H "Authorization: Basic
>> YWRtaW46YWRtaW4=" -H "Content-Type: multipart/form-data" --form
>> datasetName=TestDataset --form version=1.0.0 --form
>> description=TestDescription --form sourceType=file --form destination=file
>> --form dataFormat=CSV --form
>> file=@/home/supun/Supun/MachineLearning/data/IndiansDiabetes.csv --form
>> sourcePath=/temp*
>
>
> Also works fine when I used Chrome's REST client.
>
> However,  when I call the same service, using AJAX, the file is written
> with empty content. Follow is the sample snippet.
>
>
>> *var formData = new FormData();*
>> *  formData.append("file", fileInput[0]['files'][0],
>> 'IndiansDiabetes.csv');*
>> * formData.append("datasetName", name);*
>> * formData.append("version", version);*
>> * formData.append("description", comments);*
>> * formData.append("sourceType", dataSourceType);*
>> * formData.append("sourcePath", null);*
>> * formData.append("destination", dataTargetType);**
>> formData.append("dataFormat", dataType);*
>
>
>
>
>> *       var baseUrl = getBaseUrl(window.location.href);*
>> *       $.ajax({*
>> *           type: 'POST',*
>> *           url: baseUrl + '/api/datasets', *
>> *           contentType: "multipart/form-data",*
>> *    processData: false,*
>> *           data: formData,**           beforeSend : function(xhr) {*
>>
>> *                xhr.setRequestHeader("Authorization", "Basic " +
>> "YWRtaW46YWRtaW4=");*
>> *            },*
>> *            success : function(res){*
>> *           console.log('success');*
>> *            },*
>> *            error :  function(res){*
>> *           console.log('failed');*
>> *            }   *
>> *        });** })*
>
>
> One thing I noticed was, When calling the service using CURL and
> REST-Client, the *@Multipart("file") InputStream inputStream* binds to
> a "LoadingByteArrayOutputStream", but when calling from AJAX (or even with
> a java client) it binds to a
> "org.apache.cxf.attachment.DelegatingInputStream", and hence writing an
> empty content to the server.
>
> Any idea on how to overcome this?
>
> Thanks,
> Supun
>
> --
> *Supun Sethunga*
> Software Engineer
> WSO2, Inc.
> http://wso2.com/
> lean | enterprise | middleware
> Mobile : +94 716546324
>
> _______________________________________________
> Dev mailing list
> Dev@wso2.org
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 
Malith Munasinghe | Software Engineer
M: +94 (071) 9401122
E: mali...@wso2.com
W: http://wso2.com
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to