【发布时间】:2018-08-05 19:59:18
【问题描述】:
下面的代码是一个 API 调用,我在其中传递了一个 documentID 并尝试打开一个文档。由于整个过程都在服务器上运行,因此我无法在任何其他设备(无论是其他台式机还是移动设备)上查看该文件,尽管该文件将在服务器计算机中打开但不会在本地打开。谁能指导我了解我哪里出错了? (对不起代码,我知道它可能并不完美,因为我是网络开发新手。还在学习)。
{
int i = 1;
string key = ConfigurationManager.AppSettings["PhysicalDocumentPath"]; // some address like "xxx.xxx.xxx.xxx\folder\documents...."
string key2 = ConfigurationManager.AppSettings["PhysicalDocumentPath2"]; // "C:\{somefolder}\{somefolder}...."
JAppDoc value = new JAppDoc();
var response = new Response();
try
{
if (!Directory.Exists(key2))
{
Directory.CreateDirectory(key2);
}
IAppDataService appDataService = new AppDataService();
response = appDataService.GetDoc(docId, value);
var fileName = value.ApplicantId + "_" + value.DocumentName;
var savefileName = fileName;
var fileSavePath = Path.Combine(key, fileName);
var prevPath = fileSavePath;
var nextPath = fileSavePath;
var tmp = fileName.Split('.');
var tmp1 = tmp[0];
while (File.Exists(nextPath))
{
tmp = fileName.Split('.');
fileName = tmp1 + i.ToString();
fileName = fileName + "." + tmp[1];
savefileName = fileName;
nextPath = Path.Combine(key, savefileName);
if (File.Exists(nextPath))
{
prevPath = nextPath;
}
i++;
}
try
{
tmp = prevPath.Split(new[] { "Docs\\" }, StringSplitOptions.None);
var serverpath = key + tmp[1];
var localpath = key2+ tmp[1];
if (File.Exists(localpath))
{
Process.Start(localpath);
}
else
{
System.IO.File.Copy(serverpath, localpath);
Process.Start(localpath);
}
}
catch(Exception e)
{
Utils.Write(e);
response.Message = "File not found !";
}
}
catch (Exception ex)
{
Utils.Write(ex);
}
return Ok(response);
}
【问题讨论】:
-
您需要在响应中将文件(或者准确地说,文件的内容)发送回客户端。您可以在 Google 上搜索大量基于 WebAPI 的示例,但使用
FileResult(而不仅仅是简单的Ok())是一种简单的方法。 -
调用是通过 AngulaJS 控制器进行的。我能否将 FileResult Response 作为该文件获得?你能多指导一点吗?提前致谢。
-
您是在进行 ajax 调用还是完整的 HTTP 请求?通过 ajax 下载文件并没有真正起作用,因为它试图将文件内容发送到网页(在 Javascript 变量内),而不是将其作为标准文件下载发送到浏览器。如果您尝试通过 ajax 执行此操作,请考虑提供类似超链接的内容,该链接将在新选项卡中访问下载 URL,或者在需要时通过脚本触发相同的行为。
-
我通过 angularjs 文件中的完整 HTTP 请求调用它
-
在这种情况下应该没有问题。当然,您始终可以通过浏览器或 PostMan 等其他工具请求单独测试您的 Web API 端点。然后你开始测试真正调用它的客户端代码。
标签: asp.net angularjs asp.net-mvc