如何在 HTTP 响应中返回 HTML 文件(Azure-Functions)

我正在学习使用 azure-functions,我想知道如何在这段代码上返回 HTML 文件。(天蓝色函数的初始 python 代码)


import logging


import azure.functions as func




def main(req: func.HttpRequest) -> func.HttpResponse:

    logging.info('Python HTTP trigger function processed a request.')


    name = req.params.get('name')

    if not name:

        try:

            req_body = req.get_json()

        except ValueError:

            pass

        else:

            name = req_body.get('name')


    if name:

        return func.HttpResponse(f"Hello {name}!")

    else:

        return func.HttpResponse(

            "Please pass a name on the query string or in the request body",

            status_code=400

        )

我想要的是这样的:


return func.HttpResponse("\index.html")

我怎样才能做到这一点?


翻过高山走不出你
浏览 325回答 3
3回答

湖上湖

接受的答案不再有效。现在您需要使用上下文来找到正确的文件夹。类似下面的代码应该可以工作。import loggingimport azure.functions as func import mimetypesdef main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:    logging.info('processed request for home funciton')    filename = f"{context.function_directory}/static/index.html"    with open(filename, 'rb') as f:        mimetype = mimetypes.guess_type(filename)        return func.HttpResponse(f.read(), mimetype=mimetype[0])

慕少森

假设您正在按照官方快速入门教程Create an HTTP triggered function in Azure学习 Azure Function for Python,那么您创建了一个函数static-file来处理这些静态文件,这些文件位于static-file您想要的路径或其他路径中MyFunctionProj,index.html依此logo.jpg类推。这是我的示例代码,如下所示。import loggingimport azure.functions as funcimport mimetypesdef main(req: func.HttpRequest) -> func.HttpResponse:&nbsp; &nbsp; logging.info('Python HTTP trigger function processed a request.')&nbsp; &nbsp; name = req.params.get('name')&nbsp; &nbsp; if not name:&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; req_body = req.get_json()&nbsp; &nbsp; &nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = req_body.get('name')&nbsp; &nbsp; if name:&nbsp; &nbsp; &nbsp; &nbsp; #return func.HttpResponse(f"Hello {name}!")&nbsp; &nbsp; &nbsp; &nbsp; path = 'static-file' # or other paths under `MyFunctionProj`&nbsp; &nbsp; &nbsp; &nbsp; filename = f"{path}/{name}"&nbsp; &nbsp; &nbsp; &nbsp; with open(filename, 'rb') as f:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mimetype = mimetypes.guess_type(filename)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return func.HttpResponse(f.read(), mimetype=mimetype[0])&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; return func.HttpResponse(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Please pass a name on the query string or in the request body",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;status_code=400&nbsp; &nbsp; &nbsp; &nbsp; )浏览器中的结果如下图。我的api的文件结构static-file如下。该index.html文件的内容如下。<html><head></head><body><h3>Hello, world!</h3><img src="http://localhost:7071/api/static-file?name=logo.jpg"></img></body></html>注意:对于在本地运行,该index.html文件可以正常显示logo.jpg。如果部署到 Azure,则需要在tagcode的属性末尾添加查询参数,例如.srcimg<img src="http://<your function name>.azurewebsites.net/api/static-file?name=logo.jpg&code=<your code for /api/static-file>"></img>希望能帮助到你。

有只小跳蛙

我做的很简单,不介意内容(上传文件),它不是那样工作的:)&nbsp; if command:&nbsp; &nbsp; &nbsp; &nbsp; return func.HttpResponse(status_code=200,headers={'content-type':'text/html'},&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; body="""<!DOCTYPE html><html><body>&nbsp; &nbsp;<form enctype = "multipart/form-data" action = "returnNameTrigger?save_file.py" method = "post">&nbsp; &nbsp;<p>File: <input type = "file" name = "filename" /></p>&nbsp; &nbsp;<p><input type = "submit" value = "Upload" /></p></form></body></html>""")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python