简体   繁体   English

如何从ASP.NET Web API返回多个文件

[英]how to return multiple files from asp.net web api

I'm trying to write an asp.net Web Api Restful service by means of which I need to send (.html,.css,.js,.png images) files (and all these files are part of a little JQuery mobile application written using Visual Studio 2013 with new multi device hybrid thing .ie Cordova extensions) to the mobile client within one request. 我正在尝试编写一个asp.net Web Api Restful服务,通过该服务我需要发送(.html,.css,.js,.png图像)文件(所有这些文件都是一个小型JQuery移动应用程序的一部分)使用Visual Studio 2013和新的多设备混合事物(即Cordova扩展)编写的内容,可以在一个请求中发送给移动客户端。

I'm stuck with two questions in my head. 我脑子里有两个问题。

  1. First how to send these files to the client within one request. 首先,如何在一个请求中将这些文件发送给客户端。 Do I need to zip them up and then return to the client? 我是否需要将它们拉上拉链然后返回给客户? If zipping them up is a viable option do I need to maintain the hierarchy of folder and subfolders (inside the zip file) as is maintained in the Visual Studio project. 如果将它们压缩是可行的选择,我是否需要像Visual Studio项目中维护的那样维护文件夹和子文件夹的层次结构(在zip文件内部)。 Why I'm asking this because I'm referencing (linking) some images from images folder into .css file in CSS folder and also referencing to custom JavaScript files from scripts folder into .html files (apparently using tag) inside Visual Studio Project, so I think if I don't maintain the same hierarchy at the client side (once all the files are unzipped and extracted to the local file system at the client side) then It would be impossible for the browser to rightly display views(.html files) to the client when the client is offline. 为什么要问这个问题,因为我在Visual Studio Project中将图像文件夹中的某些图像引用(链接)到CSS文件夹中的.css文件中,并且还将脚本文件夹中的自定义JavaScript文件引用到HTML文件中(显然使用标记),所以我想如果我不在客户端维护相同的层次结构(一旦所有文件解压缩并提取到客户端的本地文件系统),那么浏览器就不可能正确显示视图(.html客户端脱机时将文件发送给客户端)。 That's what I'm trying to do with these downloaded files on the client. 这就是我要在客户端上处理这些下载文件的方式。

  2. Now the second question is how to programmatically zip these files into one .zip file so that hierarchy of folders and subfolders within .zip file stays intact. 现在,第二个问题是如何以编程方式将这些文件压缩为一个.zip文件,以使.zip文件中的文件夹和子文件夹的层次结构保持不变。 I have seen so many articles/tutorials, stackoverflow posts and ZipFile api as well as DOTNETZip api but none of these describes how to zip various files into one zip file and maintain the same hierarchy of folders and subfolders inside the .zip file as it is in Visual Studio. 我见过很多文章/教程,stackoverflow帖子和ZipFile api以及DOTNETZip api,但是这些都没有描述如何将各种文件压缩到一个zip文件中,以及如何保持.zip文件中相同的文件夹和子文件夹层次结构在Visual Studio中。

I'm new to this concept to zipping up files and serving them to mobile client so I'm not sure what I'm asking is right or there exists an alternate to returning zip file and then unzip and extract it on client side. 我对压缩文件并将其提供给移动客户端不熟悉,因此我不确定我要问的内容是否正确,或者存在返回zip文件然后在客户端解压缩并将其解压缩的替代方法。

1 - Yes, a zip file is the way to go. 1-是的,要使用zip文件。 2 - DotNetZip support this scenario. 2-DotNetZip支持此方案。 Check it out 看看这个

 using (ZipFile zip = new ZipFile())
  {
    zip.AddDirectory(@"MyDocuments\ProjectX", "ProjectX");
    zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G") ; 
    zip.Save(zipFileToCreate);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM