C# 如何从字节数组中获取url?
我有个问题。我把docx文件作为字节数组存储在数据库中。我需要获取此文件的url。Url应该像。。。但是我不知道我怎样才能到达它。我读了很多关于memorystream、filestream等的主题,但我仍然不明白如何才能做到这一点。我在ASP MVC中写入。对于ASP.NET MVC部件,您可以使用控制器的File方法返回字节数组作为文件下载,如本例所示C# 如何从字节数组中获取url?,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有个问题。我把docx文件作为字节数组存储在数据库中。我需要获取此文件的url。Url应该像。。。但是我不知道我怎样才能到达它。我读了很多关于memorystream、filestream等的主题,但我仍然不明白如何才能做到这一点。我在ASP MVC中写入。对于ASP.NET MVC部件,您可以使用控制器的File方法返回字节数组作为文件下载,如本例所示 public class HomeController : Controller { public ActionRe
public class HomeController : Controller
{
public ActionResult Download(string id)
{
byte[] fileInBytes = GetFileDataFromDatabase(id);
return File(fileInBytes, "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
id + ".docx");
}
private byte[] GetFileDataFromDatabase(string id)
{
// your code to access the data layer
return byteArray;
}
}
网址为:http://.../home/download/{someId}对于ASP.NET MVC部件,可以使用控制器的File方法返回字节数组作为文件下载,如本例所示
public class HomeController : Controller
{
public ActionResult Download(string id)
{
byte[] fileInBytes = GetFileDataFromDatabase(id);
return File(fileInBytes, "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
id + ".docx");
}
private byte[] GetFileDataFromDatabase(string id)
{
// your code to access the data layer
return byteArray;
}
}
网址为:http://.../home/download/{someId}类似这样的内容:
[HttpGet]
[Route("file/{fileId}")]
public HttpResponseMessage GetPdfInvoiceFile(string fileId)
{
var response = Request.CreateResponse();
//read from database
var fileByteArray= ReturnFile(fileId);
if (fileByteArray == null) throw new Exception("No document found");
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(new MemoryStream(fileByteArray));
response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileId+ ".docx"
};
response.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.Headers.Add("Content-Transfer-Encoding", "binary");
response.Content.Headers.ContentLength = fileByteArray.Length;
return response;
}
或者,如果您有一个Razor Mvc站点,只需使用FileResult:类似以下内容:
[HttpGet]
[Route("file/{fileId}")]
public HttpResponseMessage GetPdfInvoiceFile(string fileId)
{
var response = Request.CreateResponse();
//read from database
var fileByteArray= ReturnFile(fileId);
if (fileByteArray == null) throw new Exception("No document found");
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(new MemoryStream(fileByteArray));
response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileId+ ".docx"
};
response.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.Headers.Add("Content-Transfer-Encoding", "binary");
response.Content.Headers.ContentLength = fileByteArray.Length;
return response;
}
或者,如果您有一个Razor Mvc站点,只需使用FileResult:我想您的意思是URL在文档中?效率不是很高,但是如果您不太了解字节,您可以将文档转换为字符串,然后使用您知道如何使用的方法进行搜索。我想你是说URL在文档中?效率不是很高,但是如果您不太了解字节,您可以将文档转换为字符串,然后使用您知道如何使用的方法进行搜索。这个实现是针对一个rest api的,现在只是一个简单的解释:因为您正在从DB读取shebang,所以在响应离开控制器后,您需要让它对客户端可用,以便将它保存在内存中或客户端使用的磁盘上,更多详细信息:My bad,我使用了两个内存流:,更新此实现是针对rest api的,现在只是一个简单的解释:由于您正在从DB读取shebang,因此在响应离开控制器后,您需要将其保持在客户端可用的状态,以便将其保留在内存或磁盘上供客户端使用,更多详细信息:My bad,我使用了两个内存流:,更新执行下载后,我是否可以下载文件?我没有结果。如何获取此Url?很抱歉提出了一些愚蠢的问题,但是使用wirh文件对我来说非常困难。我只是根据默认的MVC路由使用url执行了我的示例,浏览器提示我一个下载对话框。但这只是一个示例-如果控制器不工作,请发布一些代码。在执行下载后我是否可以下载文件?我没有结果。如何获取此Url?很抱歉提出了一些愚蠢的问题,但是使用wirh文件对我来说非常困难。我只是根据默认的MVC路由使用url执行了我的示例,浏览器提示我一个下载对话框。但这只是一个示例-如果控制器不工作,请发布一些代码。