单击按钮在mvc3中打开文件 [英] Open file in mvc3 on click of button

查看:66
本文介绍了单击按钮在mvc3中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在局部视图上有一个按钮,我想在单击按钮时打开pdf文件

I have one button on my partial view,I want to open pdf file on click of button

<button type="button" class="CleanButton" name="button" onclick="ValidateFaxReports('/Verification/PreviewFax','faxReportsComments','FaxReports');">
                           Preview Fax
                       </button>


按钮的onclick我需要调用javascript函数,以检查一些验证,
``/Verification/PreviewFax''是网址,FaxReportsComments是我的表单ID,FaxReports是我在主视图中使用的div的ID.
这是我的功能


Onclick of the button I need to call javascript function, to check some validation,
''/Verification/PreviewFax'' is Url, faxReportsComments is my form Id, FaxReports is id of div which I am using in main view.
Here is my function

private string ViewReport(string file)
        {
            string errorMessage = string.Empty;
            byte[] buffer = null;
            try
            {
                if (file.ToLower().Contains(".enc"))
                {
                    buffer = VOEI.DAL.Crypto.DecryptBinaryFileToByteArray(file);
                    file = file.ToLower().Replace(".enc", String.Empty);
                }
                else
                {
                    FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
                    BinaryReader binReader = new BinaryReader(fs);

                    buffer = binReader.ReadBytes((int)fs.Length);
                    binReader.Close();
                    fs.Close();
                }
            }
            catch (Exception e)
            {
                errorMessage = e.ToString();
                return errorMessage;
            }

            try
            {
                Response.Clear();
                Response.Buffer = true;

                Response.ClearHeaders();
                Response.ClearContent();

                Response.AddHeader("content-disposition", "attachment; filename=" + System.IO.Path.GetFileName(file) + "");
                //Set the appropriate ContentType.
                Response.ContentType = "Application/pdf";

                Response.BinaryWrite(buffer);
                Response.End();
            }
            finally
            {
                //System.IO.File.Delete(file);
            }
            return errorMessage;
        }



但这不起作用,我不知道为什么吗?
我想念什么吗?

Rachana



But it''s not working, I don''t know why?
Am I missing something?

Rachana

推荐答案

我不知道这里发生了什么,因为您缺少细节.这是AJAX调用吗(不起作用)? javascript方法在哪里?在这里发出什么样的页面请求,返回什么? ActionResult类有一个名为FileResult的派生类,这是我认为您需要的(并且要调用它,而不是AJAX调用)
I have no idea what is going on here, because you''re lacking details. Is this an AJAX call ( it won''t work ) ? Where is the javascript method ? What sort of page request is being made here and what does it return ? The ActionResult class has a derived class called FileResult, which is what I think you need ( and to call it, not an AJAX call )


这篇关于单击按钮在mvc3中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆