本文介绍了用python上传文件 - 文件在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试Andy Olsen(lynda.com和video2brain)的HTML 5 Power课程的cross origin upload示例。
我可以上传,并在服务器上说:上传到服务器完成。
但该文件不在服务器上。
为什么?
感谢您的帮助。
这是我在服务器控制台中的: / p>
c:\cou> C:\python27\python CORSServer.py 9999
为0.0服务HTTP。 0.0端口9999 ...
127.0.0.1 - - [24 / Sep / 2013 10:40:28]GET /CrossOriginUpload.html HTTP / 1.1200
-
收到一个OPTIONS请求。
127.0.0.1 - - [24 / Sep / 2013 10:40:43]OPTIONS / upload HTTP / 1.1200 -
收到POST请求。
读POST数据
127.0.0.1 - [24 / Sep / 2013 10:40:50]POST / upload HTTP / 1.1200 -
上传到服务器完成
文件CORSServer.py:
#!/ usr / bin / python
从SimpleHTTPServer导入BaseHTTPServer
import SimpleHTTPRequestHandler
$ b $ class CORSRequestHandler(SimpleHTTPRequestHandler):
def do_POST(self):
print收到POST请求。
content_length = int(self.headers ['Content-length'])
bytes_read = 0
while bytes_read< content_length:
x = self.rfile.read(1)
bytes_read + = 1
print读取POST数据
message =上传到服务器完成
self.send_response(200)
self.send_header(Access-Control-Allow-Origin,http:// mydomain1:9999)
self .send_header(Content-Length,str(len(message)))
self.send_header(Content-Type,text / plain);
self.end_headers()
打印消息
self.wfile.write(message);
def do_OPTIONS(self):
print收到一个OPTIONS请求。
self.send_response(200)
self.send_header(Access-Control-Allow-Origin,http:// mydomain1:9999)
self.send_header(Access-Control -Allow-Methods,POST)
self.end_headers()
$ b if if __name__ ==__main__:
BaseHTTPServer.test(CORSRequestHandler,BaseHTTPServer.HTTPServer)
和CrossOriginUpload.html:
<!DOCTYPE html>
< head>
< title>上传资料< / title>
< style>
body {
font-family:Arial,Verdana;
}
< / style>
< script>
//跨越请求
//全局变量。
var targetLocation =http:// localhost:9999 / upload;
var messageAreaElem;
var progressBarElem;
//点击开始上传按钮。
函数doStartUpload(){
progressBarElem.value = 0;
messageAreaElem.innerHTML =;
//获取用户选择的文件。
var fileElem = document.getElementById(file);
if(fileElem.files.length == 0){
messageAreaElem.innerHTML =您必须先选择要上传的文件;
} else {
messageAreaElem.innerHTML =打开文件...;
//开始将文件读入内存。
var reader = new FileReader();
reader.onloadend = doUpload;
reader.readAsArrayBuffer(fileElem.files [0]);
}
}
//处理文件加载到内存事件。
函数doUpload(e){
messageAreaElem.innerHTML =上传文件到服务器...;
//创建一个XMLHttpRequest对象。
var xhr = new XMLHttpRequest();
//在XMLHttpRequest对象上处理上传进度事件。
xhr.upload.onprogress = function(e){
var percentUploaded = Math.floor(100 * e.loaded / e.total);
progressBarElem.value = percentUploaded;
messageAreaElem.innerHTML = percentUploaded +%uploaded;
}
//在XMLHttpRequest对象上处理下载进度事件。
xhr.onprogress = function(e){
var percentDownloaded = Math.floor(100 * e.loaded / e.total);
progressBarElem.value = percentDownloaded;
messageAreaElem.innerHTML = percentDownloaded +%下载;
}
//在XMLHttpRequest对象上处理完成的上载/下载事件。
xhr.onload = function(e){
messageAreaElem.innerHTML =Finished;
}
//处理XMLHttpRequest对象上的错误事件。
xhr.onerror = function(e){
messageAreaElem.innerHTML =发生错误;
}
//使用XMLHttpRequest对象将文件数据异步上传到服务器。
xhr.open(POST,targetLocation,true);
xhr.send(e.target.result);
}
//执行页面初始化。
函数onLoad(){
document.getElementById(startUpload)。addEventListener(click,doStartUpload,true);
messageAreaElem = document.getElementById(messageArea);
progressBarElem = document.getElementById(progressBar);
}
window.addEventListener(load,onLoad,true);
< / script>
< / head>
< body>
< h1> Ajax上传< / h1>
< input type =fileid =file/> <峰; br />
< button id =startUpload>开始上传< / button> <峰; br /><峰; br />
< progress id =progressBarvalue =0max =100>< / progress>
< span id =messageArea>< / span>
< / body>
< / html>
解决方案
此循环读取并丢弃上传的数据。
while bytes_read< content_length:
x = self.rfile.read(1)
bytes_read + = 1
I'm trying the "cross origin upload" example of the course "HTML 5 Power" by Andy Olsen (lynda.com and video2brain).
I can upload, and on the server it says "Upload to server complete".But the file is not on the server.Why ?
Thanks for helping.
Here's what I have in the server console :
c:\cou>C:\python27\python CORSServer.py 9999
Serving HTTP on 0.0.0.0 port 9999 ...
127.0.0.1 - - [24/Sep/2013 10:40:28] "GET /CrossOriginUpload.html HTTP/1.1" 200
-
Received an OPTIONS request.
127.0.0.1 - - [24/Sep/2013 10:40:43] "OPTIONS /upload HTTP/1.1" 200 -
Received a POST request.
Read POST data
127.0.0.1 - - [24/Sep/2013 10:40:50] "POST /upload HTTP/1.1" 200 -
Upload to server complete
The file CORSServer.py :
#!/usr/bin/python
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
class CORSRequestHandler(SimpleHTTPRequestHandler):
def do_POST(self):
print "Received a POST request."
content_length = int(self.headers['Content-length'])
bytes_read = 0
while bytes_read < content_length:
x = self.rfile.read(1)
bytes_read += 1
print "Read POST data"
message = "Upload to server complete"
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "http://mydomain1:9999")
self.send_header("Content-Length", str(len(message)))
self.send_header("Content-Type", "text/plain");
self.end_headers()
print message
self.wfile.write(message);
def do_OPTIONS(self):
print "Received an OPTIONS request."
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "http://mydomain1:9999")
self.send_header("Access-Control-Allow-Methods", "POST")
self.end_headers()
if __name__ == "__main__":
BaseHTTPServer.test(CORSRequestHandler, BaseHTTPServer.HTTPServer)
And CrossOriginUpload.html :
<!DOCTYPE html>
<head>
<title>Uploading Data</title>
<style>
body {
font-family: Arial, Verdana;
}
</style>
<script>
//cross-origin requests
// Global variables.
var targetLocation = "http://localhost:9999/upload";
var messageAreaElem;
var progressBarElem;
// Handles the "Start Upload" button click.
function doStartUpload() {
progressBarElem.value = 0;
messageAreaElem.innerHTML = "";
// Get the file selected by the user.
var fileElem = document.getElementById("file");
if (fileElem.files.length == 0) {
messageAreaElem.innerHTML = "You must select a file to upload first";
} else {
messageAreaElem.innerHTML = "Opening file...";
// Start reading the file into memory.
var reader = new FileReader();
reader.onloadend = doUpload;
reader.readAsArrayBuffer(fileElem.files[0]);
}
}
// Handles the "file loaded into memory" event.
function doUpload(e) {
messageAreaElem.innerHTML = "Uploading file to server...";
// Create an XMLHttpRequest object.
var xhr = new XMLHttpRequest();
// Handle "upload progress" events on the XMLHttpRequest object.
xhr.upload.onprogress = function(e) {
var percentUploaded = Math.floor(100 * e.loaded / e.total);
progressBarElem.value = percentUploaded;
messageAreaElem.innerHTML = percentUploaded + "% uploaded";
}
// Handle "download progress" events on the XMLHttpRequest object.
xhr.onprogress = function(e) {
var percentDownloaded = Math.floor(100 * e.loaded / e.total);
progressBarElem.value = percentDownloaded;
messageAreaElem.innerHTML = percentDownloaded + "% downloaded";
}
// Handle "Finished upload/download" events on the XMLHttpRequest object.
xhr.onload = function(e) {
messageAreaElem.innerHTML = "Finished";
}
// Handle "Error" events on the XMLHttpRequest object.
xhr.onerror = function(e) {
messageAreaElem.innerHTML = "Error occurred";
}
// Upload the file data asynchronously to the server, using the XMLHttpRequest object.
xhr.open("POST", targetLocation, true);
xhr.send(e.target.result);
}
// Performs page initialization.
function onLoad() {
document.getElementById("startUpload").addEventListener("click", doStartUpload, true);
messageAreaElem = document.getElementById("messageArea");
progressBarElem = document.getElementById("progressBar");
}
window.addEventListener("load", onLoad, true);
</script>
</head>
<body>
<h1>Ajax Uploads</h1>
<input type="file" id="file"/> <br/>
<button id="startUpload">Start Upload</button> <br/><br/>
<progress id="progressBar" value="0" max="100"></progress>
<span id="messageArea"></span>
</body>
</html>
解决方案
This loop reads and discards the uploaded data.
while bytes_read < content_length:
x = self.rfile.read(1)
bytes_read += 1
这篇关于用python上传文件 - 文件在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!