Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

download - How to serve a downloadable docx file created with OpenXML SDK in .Net Core

In my .net core 2.2 MVC web app, I am currently generating a .docx file using OpenXML SDK Productivity Tool.

Currently, when the user fills in the form on my View and hits Submit, the OpenXML code is triggered and the word doc is generated, and emailed to a user. What I now need is the word document file to be also available to download for that user.

I'm not sure how to re-work my code to get this to work. Below is my [HTTPPost] method:

[HttpPost]

public ActionResult AddReferralForm(ReferralForm referralForm)

{

try

{

//generate word document of the referral form

var GeneratedClassOpenXML = new OpenXMLGeneratedClass();

var wwwRoot = _env.WebRootPath;

string constructedFilename = DateTime.Now.ToString("yyyyMMdd") + "_"+ DateTime.Now.ToString("HHmm") + "_";

string fileName = referralFormsLocation + constructedFilename+".docx";

 
var emailaddressToSendForm = _context.ReferralFormEmailAddresses.FirstOrDefault();

//Generate Word Document referral form - uses Open XML SDK

GeneratedClassOpenXML.CreatePackage(fileName, referralForm, LoggedInUser, emailaddressToSendForm.EmailAddress);

//method for user to download the word doc
ExportReferral(fileName);



return RedirectToAction("Index", "Home", new { id = LoggedInUser.Id });

}

catch (Exception ex)

{

throw new Exception("Error when saving a referral - " + ex);

}

}

This method redirects the user back to Home/Index. The emailing of the generated word document is within GeneratedClassOpenXML.CreatePackage().

I thought something like this code could work, with my HTTPPost method above calling this, but this didn't work:

[HttpGet]       
public IActionResult ExportReferral(string filePath)
{
    var x = System.IO.File.ReadAllBytes(filePath);

    return File(System.IO.File.ReadAllBytes(filePath),
    contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    fileDownloadName: "MySheet.docx");

}

I am trying to look for a way to return the user to the home page and return the file content. Any way to achieve this?

Thanks

question from:https://stackoverflow.com/questions/65904517/how-to-serve-a-downloadable-docx-file-created-with-openxml-sdk-in-net-core

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Redirect to Home with path

[HttpPost]
public ActionResult AddReferralForm(ReferralForm referralForm)

{

   //Generate Word Document referral form - uses Open XML SDK
   ...
   redirect home/index with fileName 
   return RedirectToAction("Index", "Home", new { id = LoggedInUser.Id, file= fileName });

}

Index.cshtml

<!-- you can have your own style -->
<style>
    /* dialog div must be hidden */
    #basicModal {
        display: none;
    }
</style>


<div id="basicModal">
    Confirm to download ?
</div>


<script>
    $(document).ready(function () {
        const urlParams = new URLSearchParams(window.location.search);
        const downloadUrl = urlParams.get('file');

        //window.location = downloadUrl;
        if (downloadUrl != null) {
            $("#basicModal").dialog({
                modal: true,
                title: "Are you sure?",
                buttons: {
                    "YES": function () {
                        window.open(downloadUrl, '_blank');
                    },
                    "NO": function () {
                        $(this).dialog("close");
                    }
                }
            });
        }

    });
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...