上传“文件”使用Javascript [英] Upload "file" using Javascript

查看:178
本文介绍了上传“文件”使用Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是场景:

我有这个带有TextArea元素的HTML页面。



我点击一个上传按钮,我想上传textarea的内容作为一个文件。

我发现了几个例子,说明如何做一个现有的文件我试图做的是在textarea中编辑文本,然后使用Microsoft提供的REST API将文件上传到SkyDrive。

任何想法如何做到这一点?

解决方案

只要将上传按钮的动作与自定义处理程序执行AJAX的 PUT请求与内容采取从textarea。在jQuery中,它会是这样的(未经测试):
$ b

  $('#my-submit ('click',function(event){
$ .ajax({
type:'PUT',
url:'my / skydrive / url',
processData:false,
data:$('#my-textarea')。val(),
成功:function(){/ *成功处理程序* /},
错误:function(){/ *错误处理程序* /}
});
event.preventDefault();
});


Here's the scenario:

I have this HTML page with a TextArea element on it.

When I click a upload button, I would like to upload the content of the textarea as a file.

I have found several examples that shows how to do this for an existing file on disc.

What I'm trying to do is edit text in the textarea and then upload the "file" to SkyDrive using the REST API provided by Microsoft.

Any idea how this could be done?

解决方案

Just replace the upload button's action with a custom handler which performs an AJAX PUT request with content taken from the textarea. In jQuery it would be something like this (untested):

$('#my-submit-button').on('click', function(event) {
  $.ajax({
    type: 'PUT',
    url: 'my/skydrive/url',
    processData: false,
    data: $('#my-textarea').val(),
    success: function() { /* Success handler */ },
    error: function() { /* Error handler */ }
  });
  event.preventDefault();
});

这篇关于上传“文件”使用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆