用于公共文件上载的自定义HTTPRequestHandler

时间:2017-01-17 13:32:53

标签: wakanda

我试图将公共文件上传服务的HTTPRequestHandler添加到wakanda服务器。任何人都应该能够在不登录的情况下将文件发布到此服务。不幸的是,我得到了一个" 403 Forbidden"对HTTPRequestHandler发出的所有POST请求出错? GET请求工作正常但在这种情况下帮助不大; - )

目的是接收文件,处理文件并将其发送回客户端。

添加了我的请求处理程序,如下所示:

application.addHttpRequestHandler('^/fileUpload$', './backend/httpRequestHandler/file-upload.js', 'fileUpload');

非常感谢任何有关如何解决此问题的帮助。

1 个答案:

答案 0 :(得分:4)

最后让我自己用cURL工作。看起来我的Postman设置存在一些问题。

cURL命令:

curl --form "fileupload=@test.xml" http://localhost:8081/fileUpload

HTTPRequestHandler:



/**
 * file upload handler
 * @param request {HTTPRequest} http request
 * @param response {HTTPResponse} http response
 */
function fileUpload(request, response) {
  try {
    var counter = 1;
    var nameTemp;
    var files = [];
    var uploadFolder = Folder('/PROJECT/database/data/tmp/');

    var result = [];
    var newName;
    var myBinaryStream;

    // create upload folder if not existing
    if (!uploadFolder.exists) {
      uploadFolder.create();
    }
    // create file instances
    for (var i = 0; i < request.parts.length; i++) {
      files.push(new File(uploadFolder.path + request.parts[i].fileName.replace(/\s/g, '_')));
      // create result object
      result[i] = {};
      result[i].name = request.parts[i].fileName;
      result[i].type = request.parts[i].mediaType;
      result[i].size = request.parts[i].size;
    }
    // write file content
    for (var i = 0; i < files.length; i++) {
      counter = 1;
      if (!files[i].exists) {
        myBinaryStream = BinaryStream(files[i], 'Write');
        myBinaryStream.putBlob(request.parts[i].asBlob);
        myBinaryStream.close();
      } else {
        while (files[i].exists) {
          nameTemp = files[i].name.replace(/\s/g, '_');
          files[i] = new File(uploadFolder.path + files[i].nameNoExt.replace(/\s/g, '_') + counter + '.' + files[i].extension);
          newName = files[i].name;
          if (files[i].exists) {
            files[i] = new File(uploadFolder.path + nameTemp);
          }
          counter++;
        }
        myBinaryStream = BinaryStream(files[i], 'Write');
        myBinaryStream.putBlob(request.parts[i].asBlob);
        myBinaryStream.close();
        result[i].name = newName;
      }
    }
    result = JSON.stringify(result);

    // add response header
    response.contentType = 'application/json';

    return result;
  } catch (e) {
    console.log(e.stack);
    return e;
  }
}
&#13;
&#13;
&#13;