栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Javascript:正在上传文件…没有文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Javascript:正在上传文件…没有文件

为什么不只

XMLHttpRequest()
与POST一起使用?

function beginQuoteFileUnquoteUpload(data){    var xhr = new XMLHttpRequest();    xhr.open("POST", "http://www.mysite.com/myuploadhandler.php", true);    xhr.setRequestHeader("Content-type", "application/x-www-form-urlenpred");    xhr.onreadystatechange = function ()    {        if (xhr.readyState == 4 && xhr.status == 200) alert("File uploaded!");    }    xhr.send("filedata="+enpreURIComponent(data));}

服务器上的处理程序脚本仅将文件数据写入文件。

EDIT
文件上传仍然是具有不同内容类型的http帖子。您可以使用此内容类型,并用边界分隔内容:

function beginQuoteFileUnquoteUpload(data){    // Define a boundary, I stole this from IE but you can use any string AFAIK    var boundary = "---------------------------7da24f2e50046";    var xhr = new XMLHttpRequest();    var body = '--' + boundary + 'rn'  // Parameter name is "file" and local filename is "temp.txt"  + 'Content-Disposition: form-data; name="file";'  + 'filename="temp.txt"rn'  // Add the file's mime-type  + 'Content-type: plain/textrnrn'  + data + 'rn'  + boundary + '--';    xhr.open("POST", "http://www.mysite.com/myuploadhandler.php", true);    xhr.setRequestHeader(        "Content-type", "multipart/form-data; boundary="+boundary    );    xhr.onreadystatechange = function ()    {        if (xhr.readyState == 4 && xhr.status == 200) alert("File uploaded!");    }    xhr.send(body);}

如果要发送其他数据,只需在每个部分之间加一个边界,然后描述每个部分的content-disposition和content-
type标头。每个标头由换行符分隔,正文与标头由附加的换行符分隔。自然,以这种方式上传二进制数据会稍微困难一些:-)

进一步编辑:忘记提及,请确保要发送的文本“文件”中没有任何边界字符串,否则它将被视为边界。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/432461.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号