[Python] Uploading file to Anonfiles using anonfile module and FastAPI

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am currently creating a website where the user sends the file and gets uploaded to anonfile website (I am using anonfile python module). Before anonfile module, I was using mediafire module, but it didn't work with video files; so, I moved to anonfile module.

    Here is the code:

    app.py

    from typing import List
    import uvicorn
    from fastapi import FastAPI, File, Request, UploadFile
    from fastapi.templating import Jinja2Templates
    from bs4 import BeautifulSoup
    import requests
    from anonfile import AnonFile

    app = FastAPI()
    templates = Jinja2Templates(directory="templates/")

    def direct_link_anon(link):
    down_link1 = link
    r1 = requests.get(down_link1)
    soup1 = BeautifulSoup(r1.content, "html.parser")
    a_href1 = soup1.find("a", {"id": "download-url"}).get("href")
    a1 = str(a_href1)
    print(a1)
    return a1

    @app.post("/")
    async def create_upload_files(uploaded_files: list[UploadFile]):
    for files in uploaded_files:
    anon = AnonFile()
    upload = anon.upload(files.file)
    link = upload.url.geturl()
    result=direct_link_anon(link)
    download = "Download"
    return {'download': download,'result': result}


    @app.get("/")
    async def main(request: Request):
    return templates.TemplateResponse('index.html',context={'request': request})


    uvicorn.run(app)


    index.html

    <html>
    <head>
    <title>file upload</title>
    <script src="https://unpkg.com/dropzone@5/dist/min/dropzone.min.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/dropzone@5/dist/min/dropzone.min.css" type="text/css" />
    </head>
    <body>
    <form action="/" class="dropzone" id="my-great-dropzone">
    </form>
    <a id="download-link" href=""></a>
    <script>
    Dropzone.options.myGreatDropzone = { // camelized version of the `id`
    paramName: "uploaded_files", // The name that will be used to transfer the file
    maxFilesize: 20000, // MB
    uploadMultiple: false,
    maxFiles: 1,
    maxFilesize: 1000000000,
    init: function() {
    this.on("maxfilesexceeded", function(file) {
    this.removeAllFiles();
    this.addFile(file);
    });
    } ,
    chunking: true,
    createImageThumbnails: true,
    success: function(file, response){
    var abc = response['result'];
    document.getElementById('download-link').innerHTML = '<a href="' + abc + '">Download</a>';
    }
    };

    </script>
    </body>
    </html>


    The error:

    files.file is not a directory


    How can I solve this?

    Continue reading...

Compartilhe esta Página