weixin_33697898 2013-07-02 18:05 采纳率: 0%
浏览 85

使用AJAX上传XML文件

I've found a mound of questions on SO regarding uploading a file using AJAX although none of them really seem to find my needs.

What I need to do is have a user upload an XML file and have the script run through the XML file and take out the data that is in certain tags in the file and then push the data into a corresponding array which reflects the tag. So say I found a book in an xml, it would push the data into an array NewBooks.

I don't have any experience with PHP, quite honestly its confusing to me. If there is a way without PHP, that would be grand.

reader.onload = function (e) {
        console.log('reading file')
        $(document).ready(function () {
         console.log('analyzing ajax')
            $.ajax({
                type: "GET",
                dataType: "xml",
                success: function (xml) {
                    $(xml).find('book').each(function () {
                        UploadBooks.push($(this).text());
                    });            
                }
            })
        })
   console.log(UploadBooks);
}

That is the code I have although the printed UploadBooks has no elements, even though when I look into the XML file, there are clearly book tags.

  • 写回答

1条回答 默认 最新

  • weixin_33738555 2013-07-02 18:18
    关注

    Not all browsers can upload files via Ajax. Only those supporting XMLHttpRequest2. Getting that to work with jQuery (as per your example) is going to take some tricks too.

    You say you'd rather not use PHP, which would mean no point uploading a file anyway. Check out the HTML5 FileReader API if you want to try and parse the XML file on the client side. You might be able to load the file into a DOM structure to achieve what you're trying to do.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看