使用python上传文件

时间:2015-01-29 14:59:40

标签: python-2.7 upload python-requests

我对编程很陌生,我正在尝试编写一个将文件上传到网站的程序。我真的不知道我做错了什么。该文件未上传。我认为我的问题与多部分表单数据有关,但如果我错了,我不会感到惊讶。

import requests, os, sys

url = "myURL"

uploadFile = {'ILWinterConditions.kmz': open('C:\\Users\\JOC-001\\Documents\\GIS\\IDOT\\ILWinterConditions.kmz', 'rb')}

payload = {

    'fname': 'myfname',

    'lname': 'mylname',

    'org': 'myorg',

    'phone': 'myphone',

    'email': 'myemail',

    'datadescrip': 'mydatadesc',

    'uploadedfile': 'C:\\Users\\JOC-001\\Documents\\GIS\\IDOT\\ILWinterConditions.kmz'

}

r = requests.post(url, verify = False, auth=('myUsername','mypassword'), files=uploadFile, data=payload)
print r.content

以下是我从打印中获得的回复,该文件未上传

<head>

    <title>*I have censored this*</title>

    <link rel="stylesheet" href="styles.css"/>

</head>



<body>

    <script type="text/javascript">

        // Add other extensions here if we want to allow other files to be

        // uploaded.  Check is not case-sensitive.  Will also be checked on

        // the server.

        var allowedExtensions = [ "kml", "kmz", "jpg", "png", "csv" ];

        var maxSize = 12 * 1024*1024;

        var errorMessage = "Only .csv, .kml, .kmz, .jpg, or .png files up to 12MB are accepted!";



        // Checks that the file meets our criteria.  This is just for user

        // convenience-- to catch problems before they attempt to do the

        // upload.  The file will be checked again on the server too.

        function FileIsOkay() {

            var fileInput = document.getElementById('uploadedFile');



            // First check the file's extension

            var fileName = fileInput.value;

            var fileExtension = fileName.substr(fileName.lastIndexOf('.') + 1).toLowerCase();

            var extensionOK = false;

            for (var i=0; i<allowedExtensions.length; ++i) {

                if (fileExtension == allowedExtensions[i].toLowerCase()) {

                    extensionOK = true;

                    break;

                }

            }

            if (! extensionOK) {

                alert(errorMessage);

                return false;

            }



            // Now check the file's size.

            // Doesn't work in IE, but that's okay... it'll get checked on

            // the server any way.

            if (fileInput.files) {

                var file = fileInput.files[0];

                if (file) {

                    if (file.size > maxSize) {

                        alert(errorMessage);

                        return false;

                    }

                }

            }



            return true;

        }

    </script>



    <div class="title">

        <img src="webdavtitle.png"/>

    </div>



    <form method="POST" action="uploader.php" enctype="multipart/form-data" onsubmit="return FileIsOkay();" class="fileForm">

        <table class="detailsTable">

            <tr>

                <td class="inputLabel">First Name:</td>

                <td class="inputField"><input type="text" name="fname" size="40"></td>

            </tr>

            <tr>

                <td class="inputLabel">Last Name:</td>

                <td class="inputField"><input type="text" name="lname" size="40"></td>

            </tr>

            <tr>

                <td class="inputLabel">Organization/Agency:</td>

                <td class="inputField"><input type="text" name="org" size="40"></td>

            </tr>

            <tr>

                <td class="inputLabel">Phone (DSN or Com):</td>

                <td class="inputField"><input type="text" name="phone" size="40"></td>

            </tr>

            <tr>

                <td class="inputLabel">Email:</td>

                <td class="inputField"><input type="text" name="email" size="40"></td>

            </tr>

            <tr>

                <td class="inputLabel">Data Description:</td>

                <td class="inputField"><input type="text" name="datadescrip" size="40"></td>

            </tr>

            <tr>

                <td class="inputLabel">

                    Choose a .kml, .kmz, .jpg,<br/>

                    or .png file to upload:

                </td>

                <td class="inputField">

                    <input id="uploadedFile" name="uploadedfile" size="40" type="file"/>

                </td>

            </tr>

            <tr>

                <td></td>

                <td class="inputField"><input type="submit" value="Upload File"></input></td>

            </tr>

        </table>

    </form>



    <div class="footer">

        <img border="0" src="horiz_grey_line.gif">

    </div>

    <div class="footer">

        <a href="I Have censored this"><img border="0" src="contacthelp.png"></a>

    </div>

    <div class="footer">

        <a href="*I have censored this*"><img border="0" src="nc_address.gif"></a>

        <a href="*I have censored this*"><img border="0" src="nc_webpolicy.gif"></a>

    </div>

</body>

1 个答案:

答案 0 :(得分:0)

我想出了这段代码的问题。应该是

uploadFile = {'uploadedfile': open('C:\\Users\\JOC-001\\Documents\\GIS\\IDOT\\ILWinterConditions.kmz', 'rb')}

以适合表单数据。 <{1}}不需要

'payload'