Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Part of my Meteor asset upload client code. This handle the drag and drop events and calls the createAsset method.
'drop #asset-upload-dropzone': (e) ->
$( 'dropzone-hover'
new AssetUpload(e)
'dragenter #asset-upload-dropzone': (e) ->
$( 'dropzone-hover'
'dragleave #asset-upload-dropzone': (e) ->
$( 'dropzone-hover'
'dragover #asset-upload-dropzone': (e) ->
class AssetUpload
constructor: (e) ->
@fileList = e.dataTransfer.files
i = @fileList.length
while i--
@uploadFile @fileList[i]
uploadFile: (file) ->
reader = new FileReader()
reader.onload = (e) =>
@sendToServer file, reader
reader.readAsBinaryString file
sendToServer: (file, reader) -> 'createAsset', reader.result,, file.size, file.type
createAsset: (blob, title, size, type) ->
console.log 'creating asset'
console.log 'starting the creation'
if !(blob || title || size || type)
throw (new Meteor.Error 400, 'Required parameter missing')
if typeof title != 'string'
throw (new Meteor.Error 413, 'Title is not a string')
if typeof size != 'number'
throw (new Meteor.Error 413, 'Size is not a number')
if typeof type != 'string'
throw (new Meteor.Error 413, 'Type is not a string')
if !this.userId
throw (new Meteor.Error 403, 'You have to be logged in to create anything. Sorry Charlie.')
fs = __meteor_bootstrap__.require 'fs'
root = 'public/'
folder = 'assets~/'
path = root + folder + title
encoding = 'binary'
fs.writeFile path, blob, encoding, (error) ->
if error
console.log 'error', error
console.log 'writing to mongo'
date: new Date()
title: _.escape title
size: size
type: _.escape type
uri: folder + title
authorId: this.userId
removeAsset: (id) ->
folder = 'public/'
asset = Assets.findOne(id)
if !asset
console.log 'no asset available'
fs = __meteor_bootstrap__.require 'fs'
fs.unlink folder + asset.uri, (error) ->
if error
console.warn 'error: ', error
Assets.remove asset._id

so is this mean that i can grab these two file one into client and the other into server and make a template(name='assetUpload') then i can use upload file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment