Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用AJAX将docx文件从控制器传递到视图?_C#_Jquery_Ajax_Asp.net Mvc_Docx - Fatal编程技术网

C# 如何使用AJAX将docx文件从控制器传递到视图?

C# 如何使用AJAX将docx文件从控制器传递到视图?,c#,jquery,ajax,asp.net-mvc,docx,C#,Jquery,Ajax,Asp.net Mvc,Docx,Hi Stack Overflow社区,第一篇帖子,施展你的魔法 我遇到的问题是,我可以生成我的docx文件,但当我尝试返回它时,数据以我以前从未见过的格式返回?有人知道这是什么吗 从razor视图开始 //Start action $('#returnDeductionSheets').click(function () { //Retrieve date options for user to select $.ajax({ async: true,

Hi Stack Overflow社区,第一篇帖子,施展你的魔法

我遇到的问题是,我可以生成我的
docx
文件,但当我尝试返回它时,数据以我以前从未见过的格式返回?有人知道这是什么吗


从razor视图开始

 //Start action
$('#returnDeductionSheets').click(function () {

    //Retrieve date options for user to select
    $.ajax({
        async: true,
        type: 'POST',
        url: 'ReturnDeductionsSheetList',
        success: function (data) {

            if (data != null) {
                var options = data;

                //Format JSON dates into read-able date time format
                function formatDate(options) {
                    var dateString = options.substring(6);
                    var currentTime = new Date(parseInt(dateString));
                    var month = currentTime.getMonth() + 1;
                    var day = currentTime.getDate();
                    var year = currentTime.getFullYear();
                    var date = day + "/" + month + "/" + year;
                    return date;
                };

                //Check if I have more than one date, then return the options via a Bootbox view
                if (options.length > 1) {
                    bootbox.prompt({
                        title: "Select the Deductions Sheet you would like to print.",
                        inputType: 'checkbox',
                        inputOptions: [
                            {
                                text: 'Deductions commenced on ' + formatDate(options[3]),
                                value: options[2],
                            },
                            {
                                text: 'Deductions commenced on ' + formatDate(options[1]),
                                value: options[0],
                            }
                        ],
                        callback: function (result) {
                            //Pass the selected option into another AJAX method to generate the document else return to the view
                            if (result != null) {

                                $.ajax({
                                    type: 'POST',
                                    url: '@Url.Action("DeductionsSheet", "Home")?result=' + result,
                                    contentType: 'application/json; charset=utf-8',
                                    success: function (data) {
                                        if (data.fileName != "") {
                                            //window.location = "@Url.RouteUrl(new { Controller = "Home", Action = "Download" })/?file=" + data.fileName;
                                            window.location = '/Home/ContractSpecificDocuments?file=' + data.fileName;
                                        }
                                    }
                                });
                            } else {
                                return;
                            };
                        }
                    });

                }
                else {
我很高兴Bootbox代码能够正常工作,并且值被传递到控制器中的
DeclarationsSheet ActionResult
,因此我将跳转到此代码

扣减表方法(方法顶部)
该值以数组的形式进入方法,我通过[0]索引获取该数组

public ActionResult DeductionsSheet(List<object> result)
{
    //BOILER PLATE CODE
    /////////////////////////////////////////
    XceedDeploymentLicense.SetLicense();

    var dateStamp = DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss");

    _replacePatterns = new Dictionary<string, string>();

    int contractNumber = Convert.ToInt32(Request.Cookies["ContractNumber"].Value);
    int contractContibutionHistoryId = Convert.ToInt32(result[0]);
ActionResult完成并返回到AJAX方法,如下所示(与上面Razor视图块中的代码相同)

我不确定这是否是数据类型解析问题,或者是否需要序列化某些内容,我愿意接受所有建议

现在我不致力于这种方法,所以如果有人能提出一个替代方案,只要它有效,我很高兴。理想情况下,我会调用AJAX方法并将数据传递到控制器,然后不返回AJAX方法,但我还没有找到这样做的方法

我确实尝试了一种更简单的替代方法,即在Bootbox回调中,我使用jQuery触发器事件触发DeclarationsSheet ActionResult,但使用这种方法,我无法将数据传递到控制器

使用触发事件的替代方法

callback: function (result) {
    //Pass the selected option into another AJAX method to generate the document else return to the view
    if (result != null) {

        $('#deductionsSheet').trigger('click', [result]);

    } else {
        return;
    };
}

谢谢您的帮助。

您是否正在尝试将Word文档返回客户端?如果是这样,服务器端将返回一个blob,您需要使用var blobtype={type:''转换它。您确定这是您的第一篇文章吗?您是否尝试将Word文档返回到客户端?如果是这样,服务器端将返回一个blob,您需要使用var blobtype={type:''转换它。您确定这是您的第一篇文章吗?
success: function (data) {
    if (data.fileName != "") {
        //window.location = "@Url.RouteUrl(new { Controller = "Home", Action = "Download" })/?file=" + data.fileName;
        window.location = '/Home/ContractSpecificDocuments?file=' + data.fileName;
    }
}
callback: function (result) {
    //Pass the selected option into another AJAX method to generate the document else return to the view
    if (result != null) {

        $('#deductionsSheet').trigger('click', [result]);

    } else {
        return;
    };
}