public async Task <IActionResult> Download(string id) { id = Path.GetFileNameWithoutExtension(id); var userId = GetCurrentUserId(); var documentStream = await _libraryManager.DownloadDocumentAsync(id, userId); if (documentStream == null) { throw new InvalidOperationException($"Physical file missing for document '{id}' or bad id."); } byte[] documentByteArray; using (MemoryStream ms = new MemoryStream()) { documentStream.CopyTo(ms); documentByteArray = ms.ToArray(); } // WJC: Need this header to allow the browser to download from the Drive domain. HttpContext.Response.Headers.Append("Access-Control-Allow-Origin", "*"); // TODO Figure out how to removet his dependency var document = await _documentService.GetAsync(id); var mimeType = document?.GetMimeType() ?? "application/octet"; var fileName = document.FileName; return(new RangeFileContentResult( fileContents: documentByteArray, contentType: mimeType, fileDownloadName: fileName //By setting a file download name the framework will automatically add the attachment Content - Disposition header )); }