【问题标题】:Let user upload an xml file and parse it (without PHP)? [duplicate]让用户上传一个xml文件并解析它(没有PHP)? [复制]
【发布时间】:2014-12-13 08:38:47
【问题描述】:

我对 CSS(3) 和 HTML(5) 非常了解,但我目前的项目让我可以更进一步。然而,这对我来说是完全未知的领域。在我的项目中,我有一个文本区域,用户可以在其中提交一些 XML,然后我使用 jQuery 的 $.parseXML 方法对其进行解析。但是,我想添加上传 XML 文件的功能。

我想上传按钮应该是这样的:

<form name="upload-form" action="???" method="???">
    <input type="file" name="upload-field">
</form>

但是,我不知道动作和方法应该是什么样子。因为我停留在同一页面上并且不应该发生任何壮观的事情,所以我猜动作属性可以省略吗?方法属性可能是get,而不是post(1)

然后呢?我不知道如何在我的 jQuery 解析器中从上传的 XML 文档中获取数据。 (2) 另外,如何检查文件类型是否正确?这发生在服务器端吗? (3)

【问题讨论】:

  • 恐怕您无法仅使用客户端语言上传文件。如果您使用 jquery 验证文件类型(假设它是可能的),每个人都可以通过停用 javascript 来克服控制。我强烈建议您使用服务器端语言来做到这一点。
  • 看看FileReader APIUsing files from web applicationsthis question。根据您需要支持的浏览器,这看起来是可行的。
  • 我已经关闭了您的第一个问题的副本 - 呃问题:上传文件留在同一站点上。除此之外,Stackoverflow 最适合使用清晰的问题陈述(或者我经常这样说:一次一个问题)。

标签: php jquery xml parsing


【解决方案1】:

action 包含将接收表单数据的服务器端脚本的名称。使用 post 时,浏览器不会将输入字段的内容解析为 url。 服务器端脚本,例如在 php 中将接收请求,进行安全检查并保存数​​据。

<form name="upload-form" action="serversidescript.php" method="post">
  <input type="file" name="upload-field">
  <input type="submit" value="Submit">
</form>

服务器端脚本.php

<pre>
<?php
print_r($_REQUEST); // shows the array containing the form data

【讨论】:

    【解决方案2】:

    将方法留空并使用 post 方法。对文件使用 post 很重要,因为它具有安全功能并允许您上传更多数据。

    因为是上传文件,所以需要添加enctype,enctype="multipart/form-data" 否则不行

    <form name="upload-form" action="" method="post" enctype="multipart/form-data" >
        <input type="file" name="upload-field">
        <input type="submit" value="Submit">
    </form>
    

    然后要解析文件,你需要使用jquery读取xml文件,然后根据你的需要编写一个解析器函数。

    //write the custome parse function
    function parse(data){
       //code for your parse function goes here.
       // to append the file to html, I need the actual structure of the file to show you
    }
    
    //this reads the xml file using jquery
    $.ajax({
        url: 'name.xml', // name of file you want to parse
        dataType: "xml",
        success: parse, //this calls the parse function
            error: function(){alert("Error: Something went wrong");}
      });
    

    【讨论】:

      猜你喜欢
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多