首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从EC2应用程序向Web上传文件

从EC2应用程序向Web上传文件
EN

Stack Overflow用户
提问于 2017-01-22 09:01:26
回答 1查看 224关注 0票数 0

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

下面是这样的情况:当我在本地运行我的应用程序时,它的行为正常。req.body有内容,而req.file('item-image')不是空的。我现在正在EC2上运行它,问题是,req.body只是一个空对象,但是req.file(‘item-req.file’)不是空的。我尝试过不同的调试方案,请参见下面的内容:

  1. 如果我删除enctype="multipart/form-data“(我知道这是文件上传所需的,只需尝试),我就会得到我期望的req.body对象,但正如预期的那样,req.file(‘item-req.file’)是空的。
  2. 我把enctype=“多部分/表单-数据”放回去,然后尝试在没有文件的情况下发送请求,得到我期望的req.body对象。
  3. 我在请求中包含了一个图像,req.body对象是空的,但是req.file('item- image ')不是。

最奇怪的是,当我通过邮递员发送我的请求时,所有的行为都像预期的那样。我现在真的迷路了,请看下面的代码:

create_event.ejs

代码语言:javascript
复制
<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

代码语言:javascript
复制
$('#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

代码语言:javascript
复制
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);
            });
        });
    });
},

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-01-22 09:28:27

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

显然,sails.js使用跳过器,并且在输入顺序上是敏感的。我只是把文件的部分放在最后,现在起作用了。

我只是不明白为什么它在我的本地,而不是在我的ec2服务器上工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41789108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档