I would like to open Excel and Word files on the user’s PC; with the codes I have set up at the moment, I can open all PDF, TXT, and HTML files on a new web page; however, I can’t open Excel and Word files with specific applications on the user’s PC.
I have set up this code in Row_Rendered:
function Row_Rendered(): void
{
if (!empty($this->full_path->ViewValue)) {
$filePath = trim($this->full_path->ViewValue);
$encodedPath = urlencode($filePath);
$fileUrl = "http://192.168.100.21:5000/download?path=" . $encodedPath;
$this->botton->ViewValue = '<a href="' . $fileUrl . '" class="btn btn-primary" target="_blank">Apri</a>';
} else {
$this->botton->ViewValue = '<span class="text-danger">Percorso non valido</span>';
}
}
And I have activated Flask on the server with this code:
from flask import Flask, request, send_file, jsonify
import os
import urllib.parse
from flask_cors import CORS
import mimetypes
app = Flask(__name__)
CORS(app)
@app.route("/download", methods=["GET"])
def download_file():
file_path = request.args.get("path")
if not file_path:
return jsonify({"error": "Percorso non valido"}), 400
file_path = urllib.parse.unquote(file_path).replace("+", " ")
file_path = os.path.normpath(file_path)
print(f"Requested file: {file_path}")
if not os.path.exists(file_path) or ".." in file_path:
return jsonify({"error": f"File non trovato: {file_path}"}), 404
try:
mime_type, _ = mimetypes.guess_type(file_path)
if mime_type is None:
mime_type = "application/octet-stream"
return send_file(file_path, mimetype=mime_type, as_attachment=False)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=False)