Hướng dẫn how do you upload a file in python? - làm thế nào để bạn tải lên một tệp trong python?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọccgi environment. One often comes across various web applications in which the client or the users is required to upload data in the form of a file(eg. image file, an audio file, text file, etc). There are two aspects to uploading a file, as there are two sides to that particular interaction being client-side and the server-side. A form needs to be created to accept the user input in the form of a file.
    An HTML form has different attributes that you can set it to, for instance, which URL is the uploaded data is to be submitted is done through the action attribute. An enctype attribute called multi-part/form-data is required in a HTML form to upload a file. Secondly we will be required to use the input tag of HTML and set it equal to “file”. This adds a upload button in addition to an input button in the form. The below code example demonstrates it well:
     

    Bàn luận

    <html>

    Trong bài viết này, chúng tôi sẽ xem xét quá trình tải lên tệp trong Python bằng môi trường CGI. Người ta thường gặp các ứng dụng web khác nhau trong đó máy khách hoặc người dùng được yêu cầu tải lên dữ liệu dưới dạng tệp (ví dụ: tệp hình ảnh, tệp âm thanh, tệp văn bản, v.v.). Có hai khía cạnh để tải lên một tệp, vì có hai mặt của tương tác cụ thể đó là phía máy khách và phía máy chủ. Một biểu mẫu cần được tạo để chấp nhận đầu vào của người dùng dưới dạng tệp. Một biểu mẫu HTML có các thuộc tính khác nhau mà bạn có thể đặt nó thành, ví dụ, URL là dữ liệu được tải lên sẽ được gửi được thực hiện thông qua thuộc tính hành động . Một thuộc tính Enctype có tên là Multi-Part/Form-Data được yêu cầu trong biểu mẫu HTML để tải lên tệp. Thứ hai, chúng tôi sẽ được yêu cầu sử dụng thẻ đầu vào của HTML và đặt nó bằng với tệp tệp. Điều này thêm một nút tải lên ngoài một nút đầu vào trong biểu mẫu. Ví dụ mã dưới đây thể hiện nó tốt: & nbsp;

    HTML

    <body5

       <

    #!/usr/bin/python
    import cgi, os
    import cgitb; cgitb.enable()
    form = cgi.FieldStorage()
    # Get filename here.
    fileitem = form['filename']
    # Test if the file was uploaded
    if fileitem.filename:
       # strip leading path from file name to avoid
       # directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       open('/tmp/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn + '" was uploaded successfully'
    else:
       message = 'No file was uploaded'
    print """\
    Content-Type: text/html
       

    %s

    """ % (message,)
    1
    #!/usr/bin/python
    import cgi, os
    import cgitb; cgitb.enable()
    form = cgi.FieldStorage()
    # Get filename here.
    fileitem = form['filename']
    # Test if the file was uploaded
    if fileitem.filename:
       # strip leading path from file name to avoid
       # directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       open('/tmp/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn + '" was uploaded successfully'
    else:
       message = 'No file was uploaded'
    print """\
    Content-Type: text/html
       

    %s

    """ % (message,)
    2
    #!/usr/bin/python
    import cgi, os
    import cgitb; cgitb.enable()
    form = cgi.FieldStorage()
    # Get filename here.
    fileitem = form['filename']
    # Test if the file was uploaded
    if fileitem.filename:
       # strip leading path from file name to avoid
       # directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       open('/tmp/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn + '" was uploaded successfully'
    else:
       message = 'No file was uploaded'
    print """\
    Content-Type: text/html
       

    %s

    """ % (message,)
    3
    #!/usr/bin/python
    import cgi, os
    import cgitb; cgitb.enable()
    form = cgi.FieldStorage()
    # Get filename here.
    fileitem = form['filename']
    # Test if the file was uploaded
    if fileitem.filename:
       # strip leading path from file name to avoid
       # directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       open('/tmp/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn + '" was uploaded successfully'
    else:
       message = 'No file was uploaded'
    print """\
    Content-Type: text/html
       

    %s

    """ % (message,)
    4
    #!/usr/bin/python
    import cgi, os
    import cgitb; cgitb.enable()
    form = cgi.FieldStorage()
    # Get filename here.
    fileitem = form['filename']
    # Test if the file was uploaded
    if fileitem.filename:
       # strip leading path from file name to avoid
       # directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       open('/tmp/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn + '" was uploaded successfully'
    else:
       message = 'No file was uploaded'
    print """\
    Content-Type: text/html
       

    %s

    """ % (message,)
    5
    #!/usr/bin/python
    import cgi, os
    import cgitb; cgitb.enable()
    form = cgi.FieldStorage()
    # Get filename here.
    fileitem = form['filename']
    # Test if the file was uploaded
    if fileitem.filename:
       # strip leading path from file name to avoid
       # directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       open('/tmp/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn + '" was uploaded successfully'
    else:
       message = 'No file was uploaded'
    print """\
    Content-Type: text/html
       

    %s

    """ % (message,)
    3__

    html8

    #!/usr/bin/python
    import cgi, os
    import cgitb; cgitb.enable()
    form = cgi.FieldStorage()
    # Get filename here.
    fileitem = form['filename']
    # Test if the file was uploaded
    if fileitem.filename:
       # strip leading path from file name to avoid
       # directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       open('/tmp/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn + '" was uploaded successfully'
    else:
       message = 'No file was uploaded'
    print """\
    Content-Type: text/html
       

    %s

    """ % (message,)
    1>

    html8body>

    html8html>

    <

    fn = os.path.basename(fileitem.filename.replace("\", "/" ))
    3
    fn = os.path.basename(fileitem.filename.replace("\", "/" ))
    4
    fn = os.path.basename(fileitem.filename.replace("\", "/" ))
    5
    fn = os.path.basename(fileitem.filename.replace("\", "/" ))
    6
    #!/usr/bin/python
    import cgi, os
    import cgitb; cgitb.enable()
    form = cgi.FieldStorage()
    # Get filename here.
    fileitem = form['filename']
    # Test if the file was uploaded
    if fileitem.filename:
       # strip leading path from file name to avoid
       # directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       open('/tmp/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn + '" was uploaded successfully'
    else:
       message = 'No file was uploaded'
    print """\
    Content-Type: text/html
       

    %s

    """ % (message,)
    3
    fn = os.path.basename(fileitem.filename.replace("\", "/" ))
    8
     

    Hướng dẫn how do you upload a file in python? - làm thế nào để bạn tải lên một tệp trong python?

    <

    fn = os.path.basename(fileitem.filename.replace("\", "/" ))
    3<7
    fn = os.path.basename(fileitem.filename.replace("\", "/" ))
    5
    fn = os.path.basename(fileitem.filename.replace("\", "/" ))
    6
    #!/usr/bin/python
    import cgi, os
    import cgitb; cgitb.enable()
    form = cgi.FieldStorage()
    # Get filename here.
    fileitem = form['filename']
    # Test if the file was uploaded
    if fileitem.filename:
       # strip leading path from file name to avoid
       # directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       open('/tmp/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn + '" was uploaded successfully'
    else:
       message = 'No file was uploaded'
    print """\
    Content-Type: text/html
       

    %s

    """ % (message,)
    3__action has a python script that gets executed when a file is uploaded by the user. On the server end as the python script accepts the uploaded data the field storage object retrieves the submitted name of the file from the form’s “filename”. Now all the server needs to do it is read the file that has been uploaded and write it to the “fileitem”(say, ). At the end of this entire process the uploaded file now will be written to the server.
    So, the python script looks somewhat like the below code: 
     

    Python3

    Đầu ra cho mã HTML ở trên sẽ trông giống như bên dưới: & nbsp; & nbsp;

    Trong mã trên, hành động thuộc tính có tập lệnh Python được thực thi khi một tệp được người dùng tải lên. Trên đầu máy chủ khi tập lệnh Python chấp nhận dữ liệu được tải lên, đối tượng lưu trữ trường lấy lại tên đã gửi của tệp từ biểu mẫu Tên tệp FileName. Bây giờ tất cả các máy chủ cần phải làm nó là đọc tệp đã được tải lên và viết nó vào tệp File FileTem (nói,). Vào cuối toàn bộ quá trình này, tệp được tải lên bây giờ sẽ được ghi vào máy chủ. Vì vậy, tập lệnh Python trông có vẻ giống như mã bên dưới: & nbsp; & nbsp;

    >7 >8

    >9<0 <1<2<3

    <6body1body2body3body4body5body6

    <4 <5 The above python script doesn’t work on every server as every server has its own dependencies to allow running a script in their server for security reasons. For instance if using an Azure server one would need to import msvcrt which is Microsoft visual C++ runtime module to work.
     



    Để tải lên một tệp, biểu mẫu HTML phải có thuộc tính Enctype được đặt thành nhiều dữ liệu/hình thức. Thẻ đầu vào với loại tệp tạo nút "Duyệt".multipart/form-data. The input tag with the file type creates a "Browse" button.

    
    
       
       

    File:

       

    Đầu ra

    Kết quả của mã này là hình thức sau -

    Hướng dẫn how do you upload a file in python? - làm thế nào để bạn tải lên một tệp trong python?

    Ví dụ trên đã bị vô hiệu hóa một cách có chủ ý để lưu mọi người tải lên tệp trên máy chủ của chúng tôi, nhưng bạn có thể thử mã trên với máy chủ của mình.

    Đây là tập lệnh save_file.py để xử lý tải lên tệp -save_file.py to handle file upload −

    #!/usr/bin/python
    import cgi, os
    import cgitb; cgitb.enable()
    form = cgi.FieldStorage()
    # Get filename here.
    fileitem = form['filename']
    # Test if the file was uploaded
    if fileitem.filename:
       # strip leading path from file name to avoid
       # directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       open('/tmp/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn + '" was uploaded successfully'
    else:
       message = 'No file was uploaded'
    print """\
    Content-Type: text/html
       

    %s

    """ % (message,)

    Nếu bạn chạy tập lệnh trên trên UNIX/Linux, thì bạn cần chăm sóc thay thế Trình phân tách tệp như sau, nếu không trên máy Windows của bạn trên câu lệnh Open () sẽ hoạt động tốt.

    fn = os.path.basename(fileitem.filename.replace("\", "/" ))

    Hướng dẫn how do you upload a file in python? - làm thế nào để bạn tải lên một tệp trong python?

    Cập nhật ngày 31 tháng 1-2020 08:11:09

    • Câu hỏi và câu trả lời liên quan
    • Làm thế nào để tải lên tệp với selenium (python)?
    • Làm thế nào để tải lên một tệp trong Cypress?
    • Làm thế nào để tải lên một tệp bằng JSP?
    • Làm thế nào để chúng ta thực hiện tải lên tệp bằng cách sử dụng lập trình Python CGI?
    • Kiểu CSS của nút tải lên tệp với :: Bộ chọn File-Selector-Button
    • Tải lên tệp HTML kéo và giảm trình duyệt không?
    • Làm thế nào bạn có thể tải lên một tệp bằng JSP?
    • Tải tệp với PHP lên máy chủ PHP khác
    • Làm thế nào để tạo một nút tải lên tệp với HTML?
    • Làm thế nào để xử lý tải lên tệp windows bằng selenium webdriver?
    • Tải lên tệp bằng lớp selenium webdriver và java robot.
    • Sử dụng các hàng tiêu đề trong quá trình tải lên tệp phẳng trong SAP HANA
    • Sử dụng tải lên GUI để đính kèm tệp vào email trong SAP
    • Làm thế nào để tải lên một tệp trong selen mà không có hộp văn bản?
    • Làm thế nào để tải lên một tệp CSV lớn trong ứng dụng di động SAP Hybrid?

    Làm thế nào để bạn tải lên một tệp?

    Tải lên và xem tệp..
    Trên điện thoại Android hoặc máy tính bảng của bạn, hãy mở ứng dụng Google Drive ..
    Nhấn vào Thêm ..
    Nhấn vào tải lên ..
    Tìm và nhấn vào các tệp bạn muốn tải lên ..
    Xem các tệp đã tải lên trong ổ đĩa của tôi cho đến khi bạn di chuyển chúng ..

    Làm cách nào để tải lên các tệp từ Google Drive lên Python?

    Bước 1: Nhập thư viện.....
    Bước 2: OAuth làm dễ dàng.....
    Bước 3: Tải tệp lên ổ Google của bạn.....
    Bước 4: Liệt kê các tệp từ Google Drive.....
    Bước 5: Tải xuống các tệp từ Google Drive.....
    Bước 6: Tạo các tệp văn bản trong Google Drive.....
    Bước 7: Đọc nội dung của tệp văn bản trực tiếp từ Google Drive ..