使用浏览按钮将文件附加到Mandrill消息 [英] Attach Files to Mandrill Message with Browse Button
问题描述
我不确定这是否可能,但是我正在尝试使用 input type =file)从浏览器中输入htmlrel =nofollow> Mandrill API 。说实话,我不完全确定什么 document.getElementById('idOfFileInput')。value
实际返回。我知道这不是问这样一个新手问题的地方,但我到处找,我找不到(我知道你们中的一个人会狡猾,在五分钟内指向我一些文章)。除此之外,我已经得到一个.txt文件来附加消息 C:\ fakepath \test.txt
,我知道它是文件路径浏览器提供安全性。也许这是更好的问在代码:
按钮:
附件:& nbsp;& nbsp;< input type =fileid =file>
JS:
<$ p
$ {
type:text / *,
name:file_attachment,
内容:document.getElementById('file')。value
$ c $对不起,如果这是一个简单的问题掩盖其背景,但即使是这样,我有一种感觉,这个问题可能是一个很好的未来的参考,因为我已经尝试了一个星期的东西,寻找一些类型的解决方案,但我一直没能找到一个。任何想法?如果你想附加一个文件,你可以看看AJAX文件上传 $ b $
Mandrill API需要一个base64编码的文件字符串,所以如果你可以上传文件,你可以很容易地让服务器返回一个base64编码的字符串供你在API中使用。
有很多插件可以通过ajax上传文件,如果您使用的是jQuery等JavaScript库,您可以查看 jQuery文件上传 PHP 例如:
$ result = array();
$ result ['status'] ='错误';
if(isset($ _ FILES [mandrill_attachment]))
{
$ base64 = base64_encode(file_get_contents($ _ FILES [mandrill_attachment] [tmp_name]));
$ result ['base64'] = $ base64;
$ result ['status'] ='ok';
}
die(json_encode($ result));
我已经保留了这个例子的简短内容,但是您应该检查文件类型等假设它是一个好文件
和 JS
var mandrill_attachment = false;
$(。attachment)。uploadFile({
url:upload.php,
dragDrop:false,
multiple:false,
autoSubmit:true,
fileName:mandrill_attachment,
returnType:json,
onSuccess:function(files,data,xhr)
{
if data.status =='ok')
{
mandrill_attachment = data.base64;
} else {
alert('something went wrong ...');
}
}
});
I'm not sure whether this is even possible, but I'm trying to attach a file to an outgoing email with the Mandrill API from the file upload button (input type="file"
). To be honest, I'm not entirely sure what document.getElementById('idOfFileInput').value
actually returns. I know that this is not the place to ask a novice question like that, but I've looked everywhere and I can't find it (I know one of you will be sly and point me to some article within five minutes). That aside, though, I've gotten a .txt file to attach with the message C:\fakepath\test.txt
, which I know to be the file path that the browser gives for security. Maybe it's better asked in code:
Button:
Attachment(s): <input type="file" id="file">
JS:
"attachments": [
{
"type": "text/*",
"name": "file_attachment",
"content": document.getElementById('file').value
}
],
I'm sorry if this is a simple question disguised by its context, but even if it is, I have a feeling that this question could be a good future reference because I've been trying things for a week and looking for some type of solution, but I haven't been able to find one. Any ideas?
If you wanted to attach a file, you could look at AJAX file Upload
The Mandrill API requires a base64 encoded string of the file, so if you can upload the file you can easily make the server return a base64 encoded string for you to use in the API..
There are lots of plugins that will upload files over ajax and if you're using a JavaScript library like jQuery you can look at jQuery File Upload
If you're using PHP for instance:
$result = array();
$result['status'] = 'error';
if(isset($_FILES["mandrill_attachment"]))
{
$base64 = base64_encode(file_get_contents($_FILES["mandrill_attachment"]["tmp_name"]));
$result['base64'] = $base64;
$result['status'] = 'ok';
}
die(json_encode($result));
I've kept that short for the example, but you should check the file types etc, not just assuming it's an OK file
and the JS
var mandrill_attachment = false;
$(".attachment").uploadFile({
url: "upload.php",
dragDrop:false,
multiple:false,
autoSubmit:true,
fileName: "mandrill_attachment",
returnType:"json",
onSuccess:function(files,data,xhr)
{
if( data.status == 'ok' )
{
mandrill_attachment = data.base64;
} else {
alert('something went wrong...');
}
}
});
这篇关于使用浏览按钮将文件附加到Mandrill消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!