cloudinary multiple .jpg上传NODE.JS

时间:2016-11-25 18:28:17

标签: node.js express bluebird cloudinary

你们中的任何一个试图将50多张图片上传到cloundinary吗?我一直在尝试,但问题是承诺没有得到解决(即使使用.reflect()并且无法上传所有图像。根据上传的速度,它将失败30%~70%。

有没有办法让它完全异步并确保所有图片都正确上传?我正在使用的模块只是来自doc的bluebird和cloudinary模块。

package utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;


public class LocalisationService {
private static LocalisationService instance = null;
private final HashMap<String, String> supportedLanguages = new HashMap<>();

private ResourceBundle english;
private ResourceBundle italian;

private class CustomClassLoader extends ClassLoader {
    public CustomClassLoader(ClassLoader parent) {
        super(parent);

    }

    public InputStream getResourceAsStream(String name) {
        InputStream utf8in = getParent().getResourceAsStream(name);
        if (utf8in != null) {
            try {
                byte[] utf8Bytes = new byte[utf8in.available()];
                utf8in.read(utf8Bytes, 0, utf8Bytes.length);
                byte[] iso8859Bytes = new String(utf8Bytes, "UTF-8").getBytes("ISO-8859-1");
                return new ByteArrayInputStream(iso8859Bytes);
            } catch (IOException e) {
                e.printStackTrace();

            } finally {
                try {
                    utf8in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}


public static LocalisationService getInstance() {
    if (instance == null) {
        synchronized (LocalisationService.class) {
            if (instance == null) {
                instance = new LocalisationService();
            }
        }
    }
    return instance;
}


private LocalisationService() {
    CustomClassLoader loader = new CustomClassLoader(Thread.currentThread().getContextClassLoader());
    english = ResourceBundle.getBundle("localisation.strings", new Locale("en", "US"), loader);
    supportedLanguages.put("en", "English");
    italian = ResourceBundle.getBundle("localisation.strings", new Locale("it", "IT"), loader);
    supportedLanguages.put("it", "Italiano");
}

/**
 * Get a string in default language (en)
 * @param key key of the resource to fetch
 * @return fetched string or error message otherwise
 */
public String getString(String key) {
    String result;
    try {
        result = english.getString(key);
    } catch (MissingResourceException e) {
        result = "String not found";
    }

    return result;
}

/**
 * Get a string in default language
 * @param key key of the resource to fetch from localisations
 * @param language code key for language (such as "EN" for english)
 * @return fetched string or error message otherwise
 */
public String getString(String key, String language) {
    String result;
    try {
        switch (language.toLowerCase()) {
            case "en":
                result = english.getString(key);
                break;
            case "it":
                result = italian.getString(key);
                break;
            default:
                result = english.getString(key);
                break;
        }
    } catch (MissingResourceException e) {
        result = english.getString(key);
    }

    return result;
}

public HashMap<String, String> getSupportedLanguages() {
    return supportedLanguages;
}

public String getLanguageCodeByName(String language) {
    return supportedLanguages.entrySet().stream().filter(x -> x.getValue().equals(language)).findFirst().get().getKey();
}
}

1 个答案:

答案 0 :(得分:1)

宣传您的上传img功能并尝试使用它

function uploadimgAsync(img, i, itemId) {
    return new Promise(function (resolve, reject) {
        var pubID = 'az/toys/' + itemId + '/' + i;   
        cloudinary.v2.uploader.upload(img, { // works
            public_id: pubID,
            quality: 90  
        }, 
        function(err, res){
             if(err) {
                 reject(err);
             }
             resolve(res);   
        }); 
    });   
}