下载文档时获得奇怪的JSON结果

时间:2018-01-23 15:29:32

标签: c# json asp.net-web-api sqlcommand

我的过程很简单,在视图中:

function DownloadDoc(id) {
    if (!isGuid(id)) {
        ErrorDialog("#MessageDialog", "#lblError", "The value passed is not a valid GUID.", "Invalid GUID");
        return false;
    }
    var form = $('<form method="POST" action="DownloadProjectDocument" target="_blank">');
    form.append($('<input type="hidden" name="fileid" value="' + id + '">'));
    $(body).append(form);
    form.submit().remove();
    return false;
}

在控制器中:

[HttpPost]
    public void DownloadProjectDocument(string fileid)
    {
        Byte[] bytes = new byte[16 * 1024];
        DocumentModel model = Repository.GetProjectDocument(fileid);
        Response.ContentType = model.FileType;
        Response.AddHeader("content-disposition", "attachment;filename=" + model.FileName);
        Stream stream = Repository.GetProjectDocumentFile(fileid);
        MemoryStream ms = new MemoryStream();
        stream.Position = 0;
        stream.CopyTo(ms);
        bytes = ms.ToArray();
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }

该文档已下载,但由于某种原因它也发送了一个json文件,询问我是否要打开或保存。当我打开它时,它是整个项目记录的JSON结果。我无法弄清楚它来自哪里。有什么想法吗?

对DB的调用是:

public static Stream GetProjectDocumentFile(string id)
    {
        Stream fs = null;
        DataTable dt = new DataTable();
        using (SqlConnection conn = new SqlConnection(GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("GET_PROJECT_DOC_FILE"))
            {
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@id", SqlDbType.UniqueIdentifier)).Value = new Guid(id);
                using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                {
                    try
                    {
                        conn.Open();
                        adapter.Fill(dt);
                        if (dt.Rows.Count > 0)
                        {
                            byte[] pdf = (byte[])dt.Rows[0]["FileContents"];
                            fs = new MemoryStream(pdf);
                        }
                    }
                    catch (Exception e)
                    { }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        }
        return fs;
    }

确定表单是使用标准创建的:

using (Html.BeginForm("ModProject", "EandR", FormMethod.Post, new { @id = "formModProject" }))
{ }

我需要这个来适应项目的更新:

 function UpdateProject() {
    var good = true;
    if ($("#txtEvaluationDeadlineDate").val() === ''){
        $("#txtEvaluationDeadlineDate").addClass("errorClass");
        good = false;
    }
    if ($("#txtSolutionDeadlineDate").val() === ''){
        $("#txtSolutionDeadlineDate").addClass("errorClass");
        good = false;
    }
    if (!good){
        return false;
    }
    $.ajax({
        type: 'POST',
        data: $("#formModProject").serializeArray(),
        url: '@Url.Action("ModProject")',
        success: function (data) {
            if (data.Errors === "ERROR") {
                ErrorDialog("#MessageDialog", "#lblError", "There was an error encountered, please try again later.", "Error");
                return false;
            } else {
                $("#divDetails").dialog('close');
                var tbl = $("#tblProjects").DataTable();
                tbl.clear();
                tbl.ajax.reload();
            }
        }
    });
}

我只是序列化数组以获得整个模型。但如果我删除&#34;使用&#34;行和最后一个括号,下载文档的过程工作正常。非常令人费解!我知道有办法做一个&#34; preventDefault&#34;但是我该如何在函数中做到这一点?

0 个答案:

没有答案