Meteor

时间:2015-06-24 19:30:20

标签: javascript meteor

我有一个输入类型文件来上传图像。我还想为它设置一个默认图像,这样如果用户没有上传任何defaut得到保存/使用。怎么办呢?

Template.modelMeta.events({
    'submit #uploader-form': function(e, t)
    {



    file = $('#desc-model-thumb') // id for input type = file

    var fsFile = new FS.File(e.target[2].files[0]);


    ThumbFiles.insert(fsFile,function(err,thumbFile) {
        if (err) {
        sAlert.error("No image or invalid image format selected", {effect: 'flip', onRouteClose: false, stack: false, timeout: 8000, position: 'top'});
        } else {
        sAlert.success("Image has been Uploaded", {effect: 'flip', onRouteClose: false, stack: false, timeout: 4000, position: 'top'});     
        ModelFiles.update(modelId, {$set: {name: filename, about: description, thumbnail:thumbFile._id}}, function(error, res) {
            if (error) {
            sAlert.error(error.reason);
            } else {
            sAlert.success("Data about model has been saved", {effect: 'flip', onRouteClose: false, stack: false, timeout: 4000, position: 'top'});
            }
        });

        }
    }); 
    } 
});

这是一个模板

    <label for="filename" class ="input-head"> Model-Image </label>
    <p class ="help-text"> An image that will be used as a thumbnail for your model, the preferred size is 500 X 300 </p>   
    <input class="form-control model-thumb" id="desc-model-thumb" name="modelThumb" type="file">

集合

ThumbFiles = new FS.Collection ("thumbFiles", {
    stores: [
    new FS.Store.FileSystem("thumbFiles")
    ],
    filter: {
    allow: {
        contentTypes: ['image/png', 'image/jpeg', 'image/jpg']
        // extensions: ['jpg']
    }
    }   
});

1 个答案:

答案 0 :(得分:0)

如果用户没有定义默认文件,我只会链接默认文件:

defaultSAlert = {
  effect: 'flip',
  onRouteClose: false,
  stack: false,
  timeout: 8000,
  position: 'top'
}

function insertImage (fsFile, modelId, description) {
  ThumbFiles.insert(fsFile,function(err,thumbFile) {
    if (err) {
      return sAlert.error("No image or invalid image format selected", defaultSAlert);
    }
    sAlert.success("Image has been Uploaded", defaultSAlert);
    updateModelFiles({
      _id: modelId,
      thumbnail: thumbFile._id,
      name: thumbFile.original.name,
      description: description
    })
  });
}

function updateModelFiles (model) {
  ModelFiles.update(modelId,
    {
      $set: {
        name: filename,
        about: description,
        thumbnail: fileId
      }
    }, function(error, res) {
      if (error) {
        return sAlert.error(error.reason);
      }
      sAlert.success("Data about model has been saved", defaultSAlert);
    }
  );
}

Template.modelMeta.events({
  'submit #uploader-form': function(e, t) {
    file = $('#desc-model-thumb') // id for input type = file
    if(file.files.length) {
      insertImage(
        new FS.File(file.files[0]),
        undefined,
        undefined
      )
    } else {
      var defaultModel = ModelFiles.findOne({ $eq:{name: 'defualt.png'} })
      defaultModel._id = undefined
      updateModelFiles(defaultModel)
    }
  }
});

请注意我在insertImage函数的调用中使用了undefined两次。这是因为我不知道descriptionmodelId应该是什么。此外,我试着正确地缩进你的东西。它几乎不可读您发布代码的方式。并使用 2个空格进行缩进。这是惯例。