【问题标题】:issues showing uploaded images in sails在 Sails 中显示上传图像的问题
【发布时间】:2016-04-04 10:36:19
【问题描述】:
  add:function (req, res)
  {
    Airline.create({
      name:req.param('name'),
      email:req.param('email'),
      office:req.param('office'),
      phone:req.param('phone')
    },
      function airlineCreated(err, newAirline)
    {
      if(err)
      {
        console.log('Error: '+err);
        return res.negotiate(err);
      }
      else
      {
        console.log('uploading file');
        req.file('file').upload({
          maxBytes: 10000000,
          dirname: '../../assets/images/'
        },
          function whenDone(err, uploadedFiles)
          {
          if (err)
          {
            console.log(err);
            return res.negotiate(err);
          }

          // If no files were uploaded, respond with an error.
          if (uploadedFiles.length === 0)
          {
            console.log("No file");
            return res.badRequest('No file was uploaded');
          }


          // Save the "fd" and the url where the avatar for a user can be accessed
          Airline.update(newAirline.id,
            {

              // Generate a unique URL where the avatar can be downloaded.
              logoUrl: require('util').format('%s/airline/logo/%s', sails.getBaseUrl(), newAirline.id),

              // Grab the first file and use it's `fd` (file descriptor)
              logoFd: uploadedFiles[0].fd
            })
            .exec(function (err)
            {
              if (err)
              {
                return res.negotiate(err);
                console.log(err);
              }

              return res.ok();
            });

        });

      }

    });

  }

我正在学习风帆,但在上传和检索图像时遇到了问题。我按照文档进行了上传,代码在上面。下面是我上传图片后数据的json格式。

 {
    "name": "imager",
    "email": "i@m.com",
    "office": "img",
    "phone": "24232",
    "createdAt": "2016-04-04T09:06:35.404Z",
    "updatedAt": "2016-04-04T09:06:35.438Z",
    "id": 11,
    "logoUrl": "http://localhost:1337/airline/logo/11",
    "logoFd": "/Users/musaddiq/Desktop/Other proples companies/AirportAngular/airport/assets/images/6bd339f5-bc72-458c-b04c-6670bc1b59a5.jpg"
  }

我所拥有的是带有图像的个人列表,并且我有一个表格视图,我想在其中显示所有用户及其徽标。下面是我的表格视图。我不知道该怎么做,使用sails docs对我不起作用。

<div style="padding-top: 180px;" ng-app="airlineMod" ng-controller="airlineCtrl" class="container">
  <div ng-init="airlineList()">

    <table class="table table-responsive table-hover">
      <thead>
       <tr>
         <td>
           Logo
         </td>
         <td>
          Name
         </td>
         <td>
           Office
         </td>
         <td>
          Email
        </td>
         <td>
          Phone Number
        </td>
       </tr>
      </thead>
      <tbody ng-repeat="airline in airlines">
          <tr>
            <td>
             <img ng-src="{{airline.logoUrl}}" class="img-responsive img-circle" width="50" height="50">
            </td>
            <td>
              {{airline.name}}
            </td>
            <td>
              {{airline.office}}
            </td>
            <td>
              {{airline.email}}
            </td>
            <td>
              {{airline.phone}}
            </td>
          </tr>
      </tbody>
    </table>

  </div>
</div>

下面是我尝试用来获取图像的代码

 getAll:function (req, res)
  {
    Airline.find({},
      function found(err,airlines)
      {
        if(err)
        {
          return res.negotiate(err);
        }

        console.log("Airlines found");


       var SkipperDisk = require('skipper-disk');
        var fileAdapter = SkipperDisk(/* optional opts */);

        _.each(airlines,function (airline)
        {
          fileAdapter.read(airline.logoFd)
            .on('error', function (err)
            {
              return res.serverError(err);
            })
            .pipe(res);
        }); 

        return res.send(airlines);
      });
  }

我不断收到错误“fs.js:262 binding.open(pathModule._makeLong(path), TypeError: path must be a string”。

【问题讨论】:

    标签: javascript angularjs express sails.js


    【解决方案1】:
      add:function (req, res)
      {
        Airline.create({
          name:req.param('name'),
          email:req.param('email'),
          office:req.param('office'),
          phone:req.param('phone')
        },
          function airlineCreated(err, newAirline)
        {
          if(err)
          {
            console.log('Error: '+err);
            return res.negotiate(err);
          }
          else
          {
            console.log('uploading file');
            req.file('file').upload({
                maxBytes: 10000000,
                dirname: '../../assets/images',
                saveAs : _saveAs
            },
              function whenDone(err, uploadedFiles)
              {
              if (err)
              {
                console.log(err);
                return res.negotiate(err);
              }
    
              // If no files were uploaded, respond with an error.
              if (uploadedFiles.length === 0)
              {
                console.log("No file");
                return res.badRequest('No file was uploaded');
              }
    
              Airline.update(newAirline.id,
                {
    
                  logoUrl: require('util').format('%s/images/%s', sails.getBaseUrl(),uploadedFiles[0].filename)
    
              })
                .exec(function (err)
                {
                  if (err)
                  {
                    return res.negotiate(err);
                  }
    
                  return res.ok();
                });
    
            });
    
            function _saveAs(file, cb)
            {
              var newName = file.filename;
              return cb(null, newName);
            }
    
          }
    
        });
    
      }
    

    我只是将要保存的文件名从自动生成的文件名更改为原始文件名,并用它来保存 logoUrl,然后我从前端引用它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-07
      • 2022-06-21
      • 1970-01-01
      • 2013-02-13
      • 2011-02-15
      • 2012-07-15
      • 1970-01-01
      相关资源
      最近更新 更多