我有一个运行在ESP8266上的简单服务器,其中有保存文件的SD卡。在我的PC上,我正在运行wampserver,并且有一个页面可以将文件上传到ESP上的SD卡上
<!DOCTYPE html>
<html>
<head>
<title>Handle Files</title>
<h1 style="text-align:center;">
File access page</h1>
</head>
<body>
<form action="http://192.168.1.8/upload" method="post"
enctype="multipart/form-data">
<input type="text" name="ip" value="192.168.1.8">
<input type="file" name="name">
<input class="button" type="submit" value="Upload">
</form>
</body>
</html>
我有两种连接ESP的方法-作为接入点和通过WiFi。当作为接入点连接时,IP为192.168.4.1,而当WiFi为192.168.1.8时。
如何在表单操作中使用名为ip的文本框中的IP地址。
我假设我将需要javascript,并已广泛使用它来将各种数据从服务器传递到其他页面以及将其转发到其他页面,但这使我难以理解。
请注意,服务器会将重定向发送回同一页面。
迈克。 编辑,这是处理上传的服务器端代码。
void handleUpload(){
static File in;
HTTPUpload& upload = server.upload();
if(upload.status == UPLOAD_FILE_START){
String filename = upload.filename;
if(!filename.startsWith("/")) filename = "/"+filename;
Serial.print("handleUpload Name: ");
Serial.println(filename);
if(SD.exists(filename))
SD.remove(filename);
in = SD.open(filename,FILE_WRITE); // Open the file for writing
}else if(upload.status == UPLOAD_FILE_WRITE){
if(in)
in.write(upload.buf, upload.currentSize); // Write the received bytes to the file
}else if(upload.status == UPLOAD_FILE_END){
if(in) { // If the file was successfully created
in.close(); // Close the file again
Serial.print("handleUpload Size: ");
Serial.println(upload.totalSize);
server.sendHeader("Location","http://localhost/file.htm"); // Redirect the client back to local
server.send(303);
}else{
server.send(500, "text/plain", "500: couldn't create file");
}
}
}