2

Using an MVC3 .net controller on the back-end, I am performing an HttpPost to submit a file via the HttpContext.

When the server reads the file, a list of strings is returned with the upload results.

So far, I have been able to get two scenarios working:

1) Return the list of strings as a single, tab-delimited string as a JsonResult

string tabSeparatedMessages = "";

foreach (string message in messageLog)
{
    tabSeparatedMessages = tabSeparatedMessages + message;

    if (message != messageLog[messageLog.Count - 1])
    {
        tabSeparatedMessages = tabSeparatedMessages + '\n';
    }
}
return this.Json(new
{
    success = true,
    total = messageLog.Count,
    data = tabSeparatedMessages
}, "text/html", JsonRequestBehavior.AllowGet);

2) Return the list of strings in a text file as an ActionResult

string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".txt";

using (StreamWriter writer = new StreamWriter(fileName))
{
    foreach (string message in messageLog)
    {
        writer.WriteLine(message);
    }
}

return File(fileName, "text/plain", "UPLOAD_RESULTS.txt");

My problem is that I want elements from each of these methods but am having difficulty combining the two. I want the JsonResult so that I can return a boolean 'success' value to release a modal, waiting message on the client. I want the ActionResult so that the client can simply download a file instead of parsing the list of strings into the GUI components.

Is there a way to return a text file to be downloaded as part of the JsonResult or return Json data with the ActionResult?

Thank you for reading

4

2 回答 2

2

不幸的是,您一次只能返回一种结果类型。如果我是你并且我真的需要将 upload_results.txt 下载到客户端,我的 json 结果中会有一个参数,它是对服务器上文件的引用。

所以客户端首先得到上传成功的响应,然后隐藏模式对话框,然后使用 javascript 对 upload_results.txt 文件发出另一个请求。

于 2012-03-19T13:36:54.430 回答
0

我同意上述观点,发送 JSON 并将文件临时存储在网络服务器上。在 JSON 中有此文件的路径,然后执行 GET

于 2012-03-19T14:43:09.640 回答