1

关于我的问题,这个标题可能不是 100% 准确,如果是这种情况,我事先道歉。这是我的问题,我发现这个很酷的小程序允许您从剪贴板粘贴图像而无需上传。但是,我的站点没有使用明确编写的 HTML,所以我不知道如何插入这个小程序。例如,这是我的上传按钮代码,通常用于输入图像:

getUploadControl: function(data) {
        var uploadData = data.uploadData || {},
                    resultHandler = generateCallbackHandler({
                        success: data.onSuccess,
                        error: data.onError,
                        busyMethod: noop
                    }),
                    eventHandler = function(e) {
                        var jsonResponse = parseJSON(e.XMLHttpRequest.responseText);
                        resultHandler(jsonResponse);
                    };
        return extend(true, {
            type: "upload",
            options: {
                async: {
                    saveUrl: window.CaledonianAPIWebServiceRoot + "WriteFile.aspx"
                },
                multiple: false,
                upload: function(e) {
                    var ud = copyNestedProperties({}, uploadData);
                    api.addAuthToData(ud);
                    e.data = ud;
                },
                success: eventHandler,
                error: eventHandler
            }
        }, data);
    },

我想使用SUPA Applet制作一个类似的按钮。这是小程序的演示

我已经下载了 Supa.js 的源代码,就在这里,不是很长。

function Supa() {
this.ping = function (supaApplet) {
    try {
        // IE will throw an exception if you try to access the method in a 
        // scalar context, i.e. if( supaApplet.pasteFromClipboard ) ...
        return supaApplet.ping();
    } catch (e) {
        return false;
    }
};

this.ajax_post = function (actionUrl, bytes, fieldname_filename, filename, params) {
    // some constants for the request body
    //FIXME: make sure boundaryString is not part of bytes or the form values
    var boundaryString = 'AaB03x' + parseInt(Math.random() * 9999999, 10),
        boundary = '--' + boundaryString,
        cr = '\r\n',
  body,
  i,
  isAsync,
  xrequest;

    // sanity checks
    if (!fieldname_filename || fieldname_filename === "") {
        throw "Developer Error: fieldname_filename not set or empty";
    }

    if (!filename || filename === "") {
        throw "Filename required";
    }

    // build request body
    body = '';
    body += boundary + cr;

    if (isArray(params)) {
        for (i = 0; i < params.length; i += 1) {
            body += "Content-disposition: form-data; name=\"" + escape(params[i].name) + "\";" + cr;
            body += cr;
            body += encodeURI(params[i].value) + cr;
            body += boundary + cr;
        }
    }

    // add the screenshot as a file
    body += "Content-Disposition: form-data; name=\"" + escape(fieldname_filename) + "\"; filename=\"" + encodeURI(filename) + "\"" + cr;
    body += "Content-Type: application/octet-stream" + cr;
    body += "Content-Transfer-Encoding: base64" + cr;
    body += cr;
    body += bytes + cr;
    // last boundary, no extra cr here!
    body += boundary + "--" + cr;

    // finally, the Ajax request
    isAsync = false;
    xrequest = new XMLHttpRequest();
    xrequest.open("POST", actionUrl, isAsync);

    // set request headers
    xrequest.setRequestHeader("Content-Type", "multipart/form-data; charset=UTF-8; boundary=" + boundaryString);
    xrequest.send(body);

    return xrequest.responseText;
};
}

function supa() {
    return new Supa();
}

我的问题是如何制作一个类似的小部件来使用这个库进行粘贴过程?paste() 方法可以在我之前引用的演示的源 HTML 中找到,它不在主文件 Supa.js 中。另外,我没有使用 HTML,所以我可能必须将所有代码合并为一个?无论如何,感谢任何帮助,让我走上正轨。

4

0 回答 0