How do I serve a file from a REST api backend to the frontend?
- User click on download link which programmatically (using ajax) sends a POST request with authentication details is passed to REST api backend.
- Authentication check occurs.
- Some file processing occurs.
- Serve file to frontend as a download.
Point 5 is where I am having an issue. I can see the binary data is returned from the backend but no download pops up.
async function apiExportProject(projectID, fileType) {
// Create url.
let url = new URL(urlBaseAPI + "/project/export");
// Prepare POST data.
let formData = new FormData();
formData.append("sessionKey", getSessionKeyCookieValue());
formData.append("projectID", projectID);
formData.append("fileType", fileType);
// Send the POST request.
await sendPOSTRequest(url, formData);
}
//GO CODE
responseWriter.Header().Set("Content-Disposition", "attachment; filename="+strconv.Quote(postFormData.Get("projectID")))
responseWriter.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(responseWriter, request, filePath)