Python 如何将文件保存在用户本地目录而不是flask中的服务器上?

Python 如何将文件保存在用户本地目录而不是flask中的服务器上?,python,flask,file-handling,python-os,Python,Flask,File Handling,Python Os,我创建了一个web应用程序,用户可以从athena下载.xlsx格式的报告 当我在系统上托管应用程序时,用户可以访问flask的网页,但当用户下载报告时,报告会保存在我的系统中 当我尝试更改位置或其他方法时,会出现内部服务器错误 ''' ''' 我还使用了: ''' path=“C:\Flask” ''' 将错误获取为: cmd:PermissionError:[Errno 13]权限被拒绝 网页:内部服务器错误 服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。 由于我是flas

我创建了一个web应用程序,用户可以从athena下载.xlsx格式的报告

当我在系统上托管应用程序时,用户可以访问flask的网页,但当用户下载报告时,报告会保存在我的系统中

当我尝试更改位置或其他方法时,会出现内部服务器错误

'''

'''

我还使用了: ''' path=“C:\Flask”

''' 将错误获取为: cmd:PermissionError:[Errno 13]权限被拒绝 网页:内部服务器错误 服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。


由于我是flask框架的新手,描述性代码将对我帮助更大。

您需要将此
wb.save(location)
更改为用户系统的位置,而不是您的系统是,我尝试并获得了内部服务器错误创建一个新的api,该api将文件从您的系统/临时位置返回给用户可能是@TrentonMcKinney的副本-尝试此操作时(返回send_file(path,as_attachment=True))获得错误作为内部服务器错误服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序中存在错误。非常感谢你们的快速回复
CGIHandler().run(app)


app = Flask(__name__)



def colorExcle(loc):
    wb = Workbook()
    location = loc


    redFill = PatternFill(start_color='99e6ff',
                    end_color='99e6ff',
                    fill_type='solid')

    ws = wb.active
    wb = openpyxl.load_workbook(location)

    sheet = wb['Sheet1']
    sheet.cell(row=1, column=11).value = 'UserName'
    sheet.cell(row=1, column=12).value = 'Result'
    sheet.cell(row=1, column=13).value = 'Coding Time in min'
    sheet.cell(row=1, column=14).value = 'QC1 By'
    sheet.cell(row=1, column=15).value = 'QC1 Result'
    sheet.cell(row=1, column=16).value = 'QC1 Time in min'
    sheet.cell(row=1, column=17).value = 'Comments'
    sheet.cell(row=1, column=18).value = 'KM'
    sheet.cell(row=1, column=19).value = 'Rework'
    sheet.cell(row=1, column=20).value = 'QC2 Time in min'
    sheet.cell(row=1, column=21).value = 'QC2 Results'



    sheet.cell(row=1,column=1).fill = redFill
    sheet.cell(row=1,column=2).fill = redFill
    sheet.cell(row=1,column=3).fill = redFill
    sheet.cell(row=1,column=4).fill = redFill
    sheet.cell(row=1,column=5).fill = redFill
    sheet.cell(row=1,column=6).fill = redFill
    sheet.cell(row=1,column=7).fill = redFill
    sheet.cell(row=1,column=8).fill = redFill
    sheet.cell(row=1,column=9).fill = redFill
    sheet.cell(row=1,column=10).fill = redFill
    sheet.cell(row=1,column=11).fill = redFill
    sheet.cell(row=1,column=12).fill = redFill
    sheet.cell(row=1,column=13).fill = redFill
    sheet.cell(row=1,column=14).fill = redFill
    sheet.cell(row=1,column=15).fill = redFill
    sheet.cell(row=1,column=16).fill = redFill
    sheet.cell(row=1,column=17).fill = redFill
    sheet.cell(row=1,column=18).fill = redFill
    sheet.cell(row=1,column=19).fill = redFill

    sheet.cell(row=1,column=20).fill = redFill
    sheet.cell(row=1,column=21).fill = redFill
    wb.save(location)   

    return render_template('index.html')





@app.route('/', methods = ['GET', 'POST'])

def index():

    if "HAD" in request.form:

        projectTag = request.form.get('projectTag')
        region = request.form.get('region')

        if request.method == "POST":
            region = request.form.get("region", None)
            if region!=None:

                conn = connect(aws_access_key_id='xxxxxxxxxxxxxxxxxxxxxxxxx',
                    aws_secret_access_key='xxxxxxxxxxxxxxxxxxxxxxxxxxx',
                    s3_staging_dir='s3://xxxxxxxxxxxxxxxxxxxxxx/',
                    region_name='us-west-1')


                userName = getpass.getuser()

                cursor = conn.cursor()
                now = datetime.today().strftime("%d-%m-%y")
                excelName=region.upper()+"_"+projectTag+"_"+now
                wb = Workbook()
                ws = wb.active

                data1 = r"C:\\Users\\"+ getpass.getuser()+"\\Downloads\\"+excelName+'.xlsx'


                wb.save(data1)
                wb = openpyxl.load_workbook(data1)
                sheet = wb['Sheet']
                data =  pd.read_sql("SELECT * from geocord;",conn)
                data.to_excel(data1)

                return colorExcle(data1)
                return render_template('index.html', projectTag,region)


    return render_template('index.html')


@app.route('/home', methods = ['GET', 'POST'])



@app.route('/about')
def about():
    return render_template('about.html')



if __name__ == "__main__":

    app.run(host="192.168.0.103",port=80)
return send_file(path, as_attachment=True)