我正在尝试使用ajax调用(POST)从前端到后端临时上传(读取文件,然后将其丢弃。我不想保存文件。):
var data = new FormData();
data.append('file',file);
$.ajax({
url: 'http://127.0.0.1:5555/upload',type: 'POST',data: data,cache: false,processData: false,contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',success: function(result){
print(result)
}
});
文件确实到达了猎鹰后端。这是我的后端代码:
import json
import falcon
import http.client
from .services import FileReader
class Upload:
def on_post(self,req,resp):
data = req.bounded_stream.read()
file = data
observations = FileReader.GetObservationCount(file)
observationJson = json.parse('{"observations": ' + observations + '}')
resp.status = falcon.HTTP_200;
resp.body = json.dumps(observationJson)
您可能会看到,我正在使用静态函数GetObservationCount(file)。此函数包含以下代码:
def GetObservationCount(file):
wb = load_workbook(file)
wb = wb.active
observationCount = True
column = 'A'
row = 1
count = 0
while observationCount:
cell = column + str(row)
if not wb[cell].value:
observationCount = False
else:
count += 1
row += 1
return count - 1
如果执行此代码,则会出现此错误: openpyxl不支持b''文件格式(...) 我想将通过ajax调用发送的数据转换为(。)到.xslx文件。但是我似乎没有找到一种方法来完成这项工作。
我尝试将Ajax调用的'contentType'设置为false,但这没有什么区别。 我该怎么办?
谢谢。