本文介绍了使用files.upload API将Excel上载到Slack-文件上载,但已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Slack的 files.upload API上传从AJAX响应中检索到的Excel文档.我正在使用jQuery的AJAX进行上传,并且文件已正确上传,但文件已损坏.当我打开从Slack下载的文件时,它包含许多?"和其他表示编码错误的字符.下载文件,然后使用Slack的测试器工具上传,可以正常工作,不能正常运行导致腐败.

I'm using Slack's files.upload API to upload an Excel doc retrieved from an AJAX response. I'm using jQuery's AJAX for the upload and the file is uploading correctly except that it's corrupted. When I open the file downloaded from Slack, it contains many "?"s and other characters indicative of bad encoding. Downloading the file and then uploading using Slack's tester tool, works correctly and does not result in corruption.

最相关的部分是ajaxConfig;这是传递给jQuery的ajax()方法的对象.

The most relevant portion is the ajaxConfig; this is the object passed to jQuery's ajax() method.

self.getClient().runReport({
    query: queryString,
    success: function(response){
        var formEnc = new FormData();
        formEnc.append('file', new Blob([response], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}), 'filename.xls');
        self.getClient().slackFile({
            filename: 'MyReport.xls',
            channel: '%23my-channel',
            ajaxConfig: {
                method: 'post',
                data: formEnc,
                processData: false,
                contentType: false,
                mimeType: 'multipart/form-data',
                success: function(response){
                    resolve(response);
                },
                error: function(response){
                    reject(response);
                }
            }
        });
    },
    error: function(msg){
        reject(msg);
    }
});

我尝试了BlobmimeType的不同类型,但没有成功,尽管我不认为这是问题所在,因为我发送的POST请求看起来与Slack生成的POST请求相同测试器工具(该文件上传没有损坏).我怀疑问题一定出在我如何编码从runReport获得的响应(记住我可以手动下载该文件就好了).

I've tried different types for the Blob and the mimeType without success, though I don't think this is the problem because the POST request that I'm sending looks the same as the one generated by Slack's tester tool (which uploaded the file without corruption). I suspect that the problem must be in how I'm encoding the response I get from runReport (remember that I can download that file manually just fine).

更新我使用 FileSaver.js 下载响应,并且文件以相同的方式损坏,因此它与POST到Slack无关.它必须是编码.

UpdateI used FileSaver.js to download the response and the file is corrupted in the same way, so it's not related to the POST to Slack. It must be the encoding.

推荐答案

它最终是由jquery引起的编码问题.使用直线xhr时,上传工作正常.

It ended up being an an encoding problem caused by jquery. The upload works fine when using straight xhr.

var req = new XMLHttpRequest();
req.open(method, url);
req.responseType = 'arraybuffer';
req.onload = function(){
    success(req.response);
};
req.send();

这篇关于使用files.upload API将Excel上载到Slack-文件上载,但已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:27