The NPE comes from the wrong response format, I have to use
"/upload.html" to define HTML as output format.

My upload class is now:

public class UploadController extends ERXRouteController {

private static final int HTTP_BAD_REQUEST = 400;
private static final String EXPECTED_MIME_TYPE = "application/zip";

public UploadController(WORequest request) {
  super(request);
}

public WOActionResults uploadFileAction() throws Throwable {

  // TODO: handle user permissions

  if (!request().isMultipartFormData()) {
    return buildResponse("Only multipart/form-data allowed",
                         HTTP_BAD_REQUEST);
  }

  // you need an input element of type=file within the client
  // which has a name=upload

  String mimetype = (String)
            request().formValueForKey("upload.mimetype");
  String filename = (String)
            request().formValueForKey("upload.filename");
  WOInputStreamData stream = (WOInputStreamData)
            request().formValueForKey("upload");

  if (!(EXPECTED_MIME_TYPE.equalsIgnoreCase(mimetype))) {
    return buildResponse("MIMETYPE", HTTP_BAD_REQUEST);
  }

  try {

    String uploadedFileLocation = FileHandler.getTemporaryFolder() +
                       File.separator +
                       NSPathUtilities.lastPathComponent(filename);
    writeToFile(stream.inputStream(), uploadedFileLocation);

  } catch (IOException e) {
    return buildResponse("FILE", HTTP_BAD_REQUEST);
  }

  // TODO: create an EO which knows the new file

  return buildResponse("OK", WOResponse.HTTP_STATUS_OK);
}

private void writeToFile(InputStream uploadedInputStream,
                String uploadedFileLocation) throws IOException {

// upload file to the temp folder from inputStream

}

private WOResponse buildResponse(String message, int status) {
  WOResponse r = WOApplication.application()
                     .createResponseInContext(context());
  r.setContent(message);
  r.setHeader("text/html", "Content-Type");
  r.setStatus(status);
  return r;
}

}

I can access the controller from a GWT application (FormPanel), which
can react to the response values (OK, FILE, MIMETYPE) within a
SubmitCompleteHandler.

Maybe somebody finds it useful.
André


On 13.03.2018 17:07, André Rothe wrote:
> Hm, this results in a NullPointerException on response() and
> errorResponse(). Is it mandatory to have an EO name within the route?
> 
> André
> 
> On 13.03.2018 15:58, André Rothe wrote:
>> Maybe I can add a route in the Application class:
>>
>> routeRequestHandler.addRoute(new ERXRoute(null, "/upload",
>> ERXRoute.Method.Post, UploadController.class, "uploadFile"));
>>
>> And then I could implement a controller class UploadController:
>>
>> public class UploadController extends ERXRouteController {
>>
>>   private static final int HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
>>
>>   public UploadController(WORequest request) {
>>     super(request);
>>   }
>>
>>   public WOActionResults uploadFileAction() throws Throwable {
>>
>>     if (!request().isMultipartFormData()) {
>>       return errorResponse(HTTP_UNSUPPORTED_MEDIA_TYPE);
>>     }
>>
>>     // TODO: get some form parameters and values,
>>     // store the file content into the temp folder,
>>     // process the file
>>
>>     // TODO: maybe I can return the newly created EO id?
>>
>>     return response(WOResponse.HTTP_STATUS_OK);
>>   }
>> }
>>
>> ~André
>>
>> On 13.03.2018 14:07, André Rothe wrote:
>>> Hi,
>>>
>>> Is it possible to use ERRest for a file upload with multipart-form-data?
>>> I try to upload a file to the server to create an instance of an EO
>>> (instead of the createAction()).
>>>
>>> Thank you
>>> André
>>>
>>>  _______________________________________________
>>> Do not post admin requests to the list. They will be ignored.
>>> Webobjects-dev mailing list      (Webobjects-dev@lists.apple.com)
>>> Help/Unsubscribe/Update your Subscription:
>>> https://lists.apple.com/mailman/options/webobjects-dev/andre.rothe%40zks.uni-leipzig.de
>>>
>>> This email sent to andre.ro...@zks.uni-leipzig.de
>>>
>>
> 

-- 
UNIVERSITÄT LEIPZIG
Medizinische Fakultät
Zentrum für Klinische Studien Leipzig – KKS
André Rothe
CIO
Härtelstr. 16-18, 04107 Leipzig

Tel: 0341/ 97 16118
Fax: 0341/ 97 16189
WWW: http://www.zks.uni-leipzig.de

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (Webobjects-dev@lists.apple.com)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to