因此,我需要:使用$http或$resource上传文本blob作为一个文件,并使用angular.js --模仿卷曲样式的多部分/表单上传。
有一个上一篇关于这个问题的文章,我将尝试更明确的问题,希望有人可以确定我可能会误入歧途。
下面是我所知道的curl命令的一个示例,它适用于这个服务:
curl \
http://c.docverter.com/convert \
-F from=markdown \
-F to=pdf \
-F input_files[]=@<(echo hello) #or @example.md for a file来自sinatra的成功日志响应示例
{"input_files"=>[{:filename=>"api.md", :type=>"text/markdown", :name=>"input_files[]", :tempfile=>#<Tempfile:/tmp/RackMultipart20160426-3-5tb8po>, :head=>"Content-Disposition: form-data; name=\"input_files[]\"; filename=\"api.md\"\r\nContent-Type: text/markdown\r\n"}], "from"=>"markdown", "to"=>"pdf"}继续我需要做的
我需要上传标记文本(我假设使用一个blob)作为一个“文件”-我已经尝试了$resource和$http的十几种不同的配置,但没有运气。
一个典型的痛苦的日志反应后,我的100角尝试:
{"from"=>"markdown", "input_files"=>["{}"], "to"=>"pdf"}代码示例:
angular.module('app')
.factory('convert', function ($resource) {
return $resource('http://c.docverter.com/convert', null, {
pdf: {
method: 'POST',
headers: { 'Content-Type': 'multipart/form-data' },
params: {
from: 'markdown',
to: 'pdf',
'input_files[]': new Blob(['##title'], 'text/markdown')
}
}
});
});这只是我尝试过的一个例子。我已经敲了几个小时了,但运气不太好,也许这里的人对多部分表单数据有更多的了解,并能对气泡和角的缺陷有一些了解。
发布于 2016-04-26 18:41:01
好的,晚上睡个好觉,再睡一个小时,我有一个工作上传和下载:
var fd = new FormData();
var blob = new Blob([$scope.markdown], {type: 'text/markdown'});
var file = new File([blob], "abc.md");
fd.append('input_files[]', file);
fd.append('from', 'markdown');
fd.append('to', 'pdf');
$http.post('https://mydocverterserver.com/convert', fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined},
responseType: 'arraybuffer'
})
.success(function(response){
var b = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
FileSaver.saveAs(b, 'abc.docx');
})
.error(function(res){
console.log(res);
});还有点粗糙--我可能会把它清理干净,搬到工厂去,但它能用。
这是一个docx,但它也适用于减价和pdf。
FileSaver位来自:角文件保护程序
如果您像我一样正在寻找一个特定于docverter的解决方案-我解释服务器端修复这里。
https://stackoverflow.com/questions/36855944
复制相似问题