返回文件或视图的方法 [英] Method to return a file or a view

查看:98
本文介绍了返回文件或视图的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的jquery调用 GetFile 方法.我想根据某些条件生成一个文本文件.这并不总是始终生成一个文本文件.

I am using the jquery below to call a GetFile method. I want to generate a text file based on some conditions. This will not always generate a text file all the time.

如果condition = True,则生成文件

If condition = True generate the file

如果condition = False,则返回主页

If condition = False return back to the main page

现在,如果我返回,我只会得到空白页.

Right now if i do return, I just get a blank page.

我该如何写,以便考虑到我的两个条件?

How can I write this such that my two conditions are considered?

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $("#Button1").click(function () {
            var SelCat = $("#mylist").val();              
            if (SelCat != 0) {
                var data = {
                    categoryId: SelCat   
                }
                var url = '@Url.Action("GetFile", "Home")' + '?' + $.param(data);
                window.location.assign(url);
            } else {
                alert("You need to select an city");
            }
        });
    });   
</script>

public void GetFile(int categoryId)
{
    var fileName = "ErrorMessages.txt";
    StringBuilder strObj = new StringBuilder();

    for (int i = 0; i <= 20000; i++)
    {
        strObj.Append("Number:" + i + " " + "Here is the first line" );
        strObj.Append(Environment.NewLine);
    }

    strObj.ToString();

    // ...
    // Based on some conditions determine whether to return a file or not
    // return;

    Response.Clear();
    Response.ContentType = "text/plain";
    Response.AddHeader("content-disposition", "attachment;  filename=" + fileName );
    Response.Write(strObj);
    //Response.BinaryWrite(strObj);

    Response.End();
}

推荐答案

恕我直言:

  1. 将您的 GetFile 更改为 ActionResult .
  2. 是否可以写入文件
    一种.写入内存流(而不是响应)
    b.返回文件(memoryStream,文本/纯文本");
  3. 如果无法编写
    一种. return Redirect(〜/Main/Page");
  1. Change your GetFile to be an ActionResult.
  2. If the file can be written
    a. Writing to a memory stream (instead of response)
    b. return File(memoryStream, "text/plain");
  3. If it can't be written
    a. return Redirect("~/Main/Page");

伪代码:

public ActionResult GetFile(Int32 CategoryId)
{
  if (/*can be written*/)
  {
    using (MemoryStream stream = new MemoryStream())
    {
      using (StreamWriter writer new StreamWriter(stream))
      {
        /* writer.Write(...); */
      }
      return File(stream, "text/plain");
    }
  }
  return Redirect("~/main/Page"); // or redirectToRoute/RedirectToAction
}

供参考: File()响应方法

And for reference: File() response methods

FWIW:您可以继续使用 StringBuilder 并将其传递给 File()重载之一,我想我只是更喜欢流.:shrug:

FWIW: You can continue to use the StringBuilder and pass that to one of the File() overloads, I guess I just prefer streams. :shrug:

这篇关于返回文件或视图的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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