node.js - iOS,Nodejs图片上传问题
阿神
阿神 2017-04-17 17:32:36
0
2
173

ios端代码:

let image = goodPhoto[0]
let imageData = UIImageJPEGRepresentation(image, Constants.goodPhotoCompressionQuality)
        
if let imageData = imageData {
   uploadGoodPhotoWithImageData(imageData)
}

func uploadGoodPhotoWithImageData(imageData: NSData) {
    
    let header: [String : String] = [
        "Authorization": "Bearer \(Provider.readToken())"
    ]
    
    let name = "good"
    
    let filename = "good.jpg"
    
    Alamofire.upload(.POST, URLString.uploadURLStr + "/good", headers: nil,
        multipartFormData: {
            multipartFormData in
            
            multipartFormData.appendBodyPart(data: imageData, name: name, fileName: filename, mimeType: "image/jpeg")
        },
        encodingCompletion: {
            encodingResult in
            
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON{ response in
                    debugPrint(response)
                }
            case .Failure(let encodingError):
                print("Upload failed with error: \(encodingError)")
            }
        }
    )
}

后台代码:

var express = require("express");
var multer = require('multer');
var app = express();

function handleError(res, err) {
    return res.json(500, err);
};

var storage = multer.diskStorage({
    destination: function (req, file, callback) {
        console.log('b');
        callback(null, './uploads');
    },
    filename: function (req, file, callback) {
        console.log('b');
        callback(null, file.fieldname + '-' + Date.now());
    }
});

// var upload = multer({dest: 'photo/good/'}).single('good');
var upload = multer({storage: storage}).single('good');

exports.uploadGood = function (req, res) {

    upload(req, res, function(err) {
        console.log(err);
        if (err) {
            return handleError(res, err);
        }
        res.json(200, {msg: "File is uploaded"});
    });
};

后台报了500错误,我console.log(err)是这样的

求大神解答

阿神
阿神

闭关修行中......

reply all(2)
大家讲道理

The path cannot be found, maybe you have not created a new uplaods directory

迷茫

multer I have never used it. I took a quick look and it is based on busboy. So is this file written directly to the static file server/cache or stored in MongoDB through GridFS? If it is an operation of saving static files, will it be? Permissions issue?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template