【问题标题】:Uploading file to EC2 Web Server from Web Application从 Web 应用程序将文件上传到 EC2 Web 服务器
【发布时间】:2017-06-06 22:40:59
【问题描述】:

我正在使用 Sails.js 创建一个简单的应用程序。它的基本作用是创建一个新项目,然后将其保存到数据库并将图像上传到 S3。

会发生以下情况: 当我在本地运行我的应用程序时,它运行良好。 req.body 有内容,req.file('item-image') 不为空。 我现在在 EC2 上运行它,问题是 req.body 只是一个空对象,但 req.file('item-image') 不是空的。我尝试了不同的调试方案,请看下面:

  1. 如果我删除 enctype="multipart/form-data" (我知道这是文件上传所需要的,只是尝试),我得到我期望的 req.body 对象,但正如预期的那样,req .file('item-image') 为空。

  2. 我放回了 enctype="multipart/form-data" 然后尝试在没有文件的情况下发送请求,我得到了预期的 req.body 对象。

  3. 我在请求中包含了一张图片,req.body 对象为空,但 req.file('item-image') 不是。

最奇怪的是,当我通过邮递员发送请求时,一切都按预期进行。我现在真的迷路了,请看下面的代码:

create_event.ejs

<form action=<%= event.createUrl %> method="POST" id="form-item" enctype="multipart/form-data">
<div class="modal-dialog">
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="blue bigger">Please fill the following form fields</h4>
    </div>

    <div class="modal-body overflow-visible">
      <div class="row">
        <div class="col-xs-12 col-sm-5">
          <div class="space"></div>

          <input type="file" name="item-image"/>
        </div>

        <div class="col-xs-12 col-sm-7">
          <div class="form-group">
            <label for="form-field-username">Name</label>

            <div>
              <input class="input-large" type="text" id="form-field-username" placeholder="Item Name" name="item-name" />
            </div>
          </div>

          <div class="space-4"></div>

          <div class="form-group">
            <label for="form-field-username">Description</label>

            <div>
              <input class="input-large" type="text" id="form-field-username" placeholder="Item Description" name="item-description"/>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="modal-footer">
      <button class="btn btn-sm" data-dismiss="modal">
        <i class="icon-remove"></i>
        Cancel
      </button>

      <button class="btn btn-sm btn-primary" id="save-item">
        <i class="icon-ok"></i>
        Save
      </button>
    </div>
  </div>
</div>
</form>

create_event.js

$('#save-item').on('click', function(e){
        var response = confirm('Are you sure you want to continue saving this item?');
        if(response == true) {
            $('#form-item').submit();
        }
    });

AdminController.js

createItem: function(req, res){
    console.log('Saving..');
    console.log(req.body);
    var eventId = req.path.split('/')[4];
    req.file('item-image').upload(function callback(error, uploadedFile){
        if(error) {
            console.log(error);
            return res.serverError();
        }
        console.log(uploadedFile);

        s3Helper.upload(uploadedFile[0], function(error, data){
            if(error) {
                return res.serverError();
            }

            var item = {
                ITEM_ID: uuid.v1(),
                EVENT_ID: req.path.split('/')[4],
                NAME: req.body['item-name'],
                DESCRIPTION: req.body['item-description'],
                IMAGE_URL: data.Location
            }

            EventItem.create(item, function(error, data){
                if(error) {
                    console.log(error);
                    return res.serverError();
                }
                console.log('Successfully saved data: ');
                console.log(data);
                return res.redirect('/admin/events/' + eventId);
            });
        });
    });
},

谢谢!

【问题讨论】:

  • 您是否在启动 EC2 实例时附加了具有 S3 权限的 IAM 角色?
  • 是的,它有足够的权限上传和访问 S3 文件

标签: node.js amazon-ec2 upload sails.js multipartform-data


【解决方案1】:

我在这里找到了答案https://github.com/balderdashy/sails/issues/2508

显然,sails.js 使用了 skipper 并且对输入的顺序很敏感。我只是把文件部分放在最后,它现在可以工作了。

我只是对为什么它可以在我的本地而不是在我的 ec2 服务器上工作感到困惑。

【讨论】:

    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多