使用自己的Web服务api上传文件

时间:2015-08-12 09:07:32

标签: javascript html ajax

我尝试通过post和ajax上传文件,但我只是要更改URL。

我不希望像这样更改网址 http://localhost:8080/webportal.html?file=C%3A%5CUsers%5CVM9%5CDesktop%5Ctest5.txt

我希望上传文件

这是我的代码......

HTML

<form id="uploadForm">
    <label>Upload File:</label><br/>
    <input name="file" type="file" />
    <input type="submit" value="Submit" />
</form>
JavaScript
function UploadTestCase()
{
    $(document).ready(function (e) {
        $("#uploadForm").on('submit',(function(e) {
            e.preventDefault();
            var requireData = new FormData(this);
            $.ajax({
                url: "http://ip/webapi/upload?project=test", 
                type: "POST",
                data: requireData,
                contentType: false,
                cache: false,
                processData: false
           })
           return false;
        }));
    });
}

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Untitled Document</title>
            <script type="text/javascript" src="jquery.min.js"></script>
            <script type="text/javascript">
            $(function(){
                $('#uploaad_file').on('submit', function(e){
                    var data = new FormData(this);

                    e.preventDefault();
                    $.ajax({
                        url: 'index.php',
                        data : data,
                        processData:false,
                        contentType: false,
                        type: 'POST',           
                        success: function(){
                            console.log(r);
                        },
                        error: function(){
                            console.log(arguments);
                        }
                    })
                    return false;
                })
            })
            </script>
            </head>

            <body>
            </body>
            <form id="uploaad_file" enctype="">
            <input type="file" name="file" id="file">
            <input type="submit" value="upload">
            </form>
            </html>

我使用过这段代码。它成功地将表单数据发布到我的服务器端。我可以用print_r查看$ _FILES数组。这一切看起来都很完美。