我已经使用sails.js建立了一个带有管理面板的站点并将其部署在Heroku 上。
然后我用 Skipper 上传了图片。当我尝试:
fs.readdir "#{__dirname}/../../assets/images/Projects", (err, file)->
console.log file
我可以在文件夹中看到我的文件。
但我无法使用 ajax 或 img 标签获得它。这是要上传的代码(如果您能帮助我优化此代码,我会非常满意):
create: (req, res, next)->
project_id = req.param('id')
res.setTimeout(0)
if !project_id
res.set('Content-Type', 'text/html; charset=utf-8')
return res.badRequest('No id provided.')
images_name = []
files_counter = 0
fileName = (__newFileStream, cb)->
name = uuid.v4() + path.extname(__newFileStream.filename)
# Saving names in array to insert it database
images_name.push name
cb null, name
uploadFile = req.file('uploadFile')
uploadFile.upload {dirname: "#{__dirname}/../../assets/images/Projects", saveAs: fileName}, (err, files)->
return res.serverError(err) if err
if uploadFile.isNoop
res.set('Content-Type', 'text/html; charset=utf-8')
return res.badRequest('No images provided!')
for image,index in files
nImage = {}
# Inserting name from array to database
nImage.image = images_name[index]
nImage.project_id = project_id
ProjectImages.create nImage, (error, image) ->
if error
res.status 400
return res.json error
files_counter++
if files_counter == files.length
res.status 200
return res.send "Images upload done"