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 Show Cải thiện bài viết Lưu bài viết 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. Bàn luận
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
#!/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/html1 #!/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/html2 #!/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/html3 #!/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/html4 #!/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/html5 #!/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/html3__
#!/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/html1 >
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/html3 fn = os.path.basename(fileitem.filename.replace("\", "/" ))8
fn = os.path.basename(fileitem.filename.replace("\", "/" ))3 < 7fn = 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/html3__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;
Để 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. Đầu raKết quả của mã này là hình thức sau - 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 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("\", "/" ))
Cập nhật ngày 31 tháng 1-2020 08:11:09
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 .. |