如何从asp.net网页API返回的多个文件 [英] how to return multiple files from asp.net web api

查看:357
本文介绍了如何从asp.net网页API返回的多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过它我需要发送格式(.html,的.css,.js文件,png格式图片)文件(和所有这些文件是一个小的组成部分写一个asp.net网页API RESTful服务jQuery Mobile的应用程序使用Visual Studio 2013新的多设备混合事情.IE科尔多瓦扩展)添加到移动客户端一个请求中写的。

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 .i.e. Cordova extensions) to the mobile client within one request.

我被困在我的头两个问题。

I'm stuck with two questions in my head.


  1. 首先如何将这些文件的一个请求中发送到客户端。我是否需要压缩起来,然后返回给客户端?如果荏苒起来是一个可行的选择,我需要保持文件夹和子文件夹的层级(zip文件中)为保持在Visual Studio项目。为什么我问这个,因为我引用(链接)从图像中一些图像文件夹到.css文件在CSS文件夹中,也引用到自定义JavaScript文件从脚本文件夹到.html文件(显然使用标签)Visual Studio项目里面,所以我想如果我不保持在客户端(一旦所有的文件解压缩并提取到客户端的本地文件系统)在同一层次,然后这将是不可能的浏览器中正确显示视图(html的文件)到客户端,当客户端处于脱机状态。这就是我想要在客户端上这些下载的文件做。

  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. 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. That's what I'm trying to do with these downloaded files on the client.

现在的第二个问题是如何以编程方式压缩这些文件到一个.zip文件,这样的.zip文件中的文件夹和子文件夹的层次结构保持不变。我见过这么多的文章/教程,计算器职位和API使用ZipFile以及DOTNETZip API,但这些都不介绍如何压缩各种文件合并成一个zip文件和文件夹,保持和子文件夹的.zip文件内相同的层次结构,因为它是在Visual Studio中。

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. 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.

我是新来这个概念来压缩和解的文件,并将其投放到移动客户端,所以我不知道我问的是对还是存在一个交替在返回zip文件,然后解压缩和解压的客户端一面。

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.

宝贵的建议和意见将是非常美联社preciated。

Valuable suggestions and comments will be highly appreciated.

推荐答案

1 - 是的,一个zip文件是要走的路。
2 - DotNetZip支持此方案。 检查出来

1 - Yes, a zip file is the way to go. 2 - DotNetZip support this scenario. 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);
  }

这篇关于如何从asp.net网页API返回的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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