我把我的python flask API放在一个虚拟机上,我使用IIS webserver。当我从我的前端发送请求时,文件被上传,但转换失败。(如果我尝试在localhost上做,则工作正常)。有什么办法可以解决吗?
服务器,上传路线。
@app.route("/upload", methods=["POST"])
def upload():
file = request.files['file']
id = request.form['id']
uid = request.form['uid']
tool = request.form['tool']
Path(f"./uploads/{id}").mkdir(parents=True, exist_ok=True)
extension = os.path.splitext(file.filename)[1]
file.save(f"./uploads/{id}/{uid}{extension}")
if tool == "/jpg-to-pdf":
img_to_pdf(f"./uploads/{id}/{uid}{extension}", id)
elif tool == "/excel-to-pdf":
excel_to_pdf(f"./uploads/{id}/{uid}{extension}")
Conversion script:
import comtypes.client
import pythoncom
import os
def excel_to_pdf(file):
pythoncom.CoInitialize()
out_file = os.path.splitext(os.path.abspath(file))[0] + '.pdf'
in_file = os.path.abspath(file)
print(in_file)
excel = comtypes.client.CreateObject('Excel.Application')
excel.Visible = False
doc = excel.Workbooks.Open(in_file)
doc.ExportAsFixedFormat(0, out_file, 1, 0)
doc.Close()
excel.Quit()
Error:
Exception on /upload [POST]
Traceback (most recent call last):
File "C:\Users\Deschizatorul\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Deschizatorul\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Deschizatorul\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\Users\Deschizatorul\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Deschizatorul\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "C:\inetpub\wwwroot\flask-server\.\server.py", line 63, in upload