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?
The package your are using to upload the file doesn't seem to support passing a file-like
object (which is the result of calling the .file
attribute of the UploadFile
object). As can been in the source code here, the upload
function instead accepts a path
to the file in either str
or Path
format.
Hence, you can either save the file to a local directory on your disk and then pass the filepath to the upload
function, as described in this answer and this answer, or save the file to a NamedTemporaryFile
, which unlike SpooledTemporaryFile
that UploadFile
provides (using the .file
attribute), "has a visible name in the file system"—which you can get using the .name
attribute—and "can be used to open the file". Related answers with examples can be found here, here and here, as well as in this answer and this answer.