0

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

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

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.

ekad
  • 14,436
  • 26
  • 44
  • 46
user2913184
  • 590
  • 10
  • 33
  • See here: http://stackoverflow.com/questions/12266422/whats-the-best-way-to-serve-up-multiple-binary-files-from-a-single-webapi-metho – Keith Nov 05 '15 at 18:23

1 Answers1

0

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);
  }
Moti Azu
  • 5,392
  • 1
  • 23
  • 32
  • Thanks for the comment but does it make sense on my part to maintain folder hierarchy the way it is in Visual Studio and then unzip and extract it on the client side using local file system? – user2913184 Oct 26 '14 at 17:37
  • And additionally I just don't want to archive the entire project just some of the files like I said .js,.html,.css,.png and exclude folders like bin, and files like .suo etc. – user2913184 Oct 26 '14 at 17:45
  • Whether you unzip at the client machine is up to, but I must say this is highly irregular. Can you explain briefly what you are trying to achieve? About the partial-project-in-zip-thing, that may mean you will have to write the code that builds the desired hierarchy (which should be a tough task). – Moti Azu Oct 27 '14 at 07:20
  • Actually I wrote the code that gives me the desired hierarchy (for the time being just hard coded it and zipping up only those files that I'm going to send to the client). The thing is that I want to download this zip on JQuery Mobile client extract it and save to the local file system so that if the user is offline he can still make use of these downloaded files to work with. I explored the manifest file as well but whatever JavaScript API (webstorage,appcache) once the browsers gets cleared of any reason I end up losing all the files save locally using earlier mentioned apis. – user2913184 Oct 27 '14 at 15:04
  • So I have no other way but to get these files as a bundle through a webapi service and save them on client side and just don't care about if the browser cache is cleared or not. The thing is I don't want to give any control to the user to delete the downloaded files by clearing browser history or cache. – user2913184 Oct 27 '14 at 15:06
  • This is a very strange behavior you are looking to achieve. jQuery mobile is just a js lib for presenation, it can't do the stuff you talk about. Why do you need the user to download files to be available offline? Is it a website or a web application? Are you trying to run a website offline like a mobile app? – Moti Azu Oct 27 '14 at 15:22
  • Unfortunately I tried other ways as I said webstorage and appcache but everything gets wiped off once browser cache/history gets deleted. So I thought of this as a option may be my last resort. Yeah I'm trying to run a mobile app offline though the way I'm doing it looks like I'm making a web app work offline. But as I repeatedly mentioned that manifest and webstorage/appcache apis are not helping me and giving lot of control to the user which I don't want. – user2913184 Oct 27 '14 at 15:27
  • and then to unzip and extract those bundled files I'm using Cordova extension apis inside visual studio as you said that JQuery can't achieve what I'm trying to. – user2913184 Oct 27 '14 at 15:33
  • Running webapps offline is a big subject and I suggest you don't try to invent a solution for it yourself, look it up. As far as the question you asked goes, it is answered and any further discussion you want to have about this should happen in a new thread. – Moti Azu Oct 27 '14 at 15:35
  • I could have used the existing stuff as I mentioned like manifest file and JavaScript apis but they are not giving me desired results and offering user a lot of control. You are right I will try to open a new thread and you are most welcome to contribute in there. Thanks. – user2913184 Oct 27 '14 at 15:39
  • 1
    you can contribute to a new thread in here http://stackoverflow.com/questions/26591643/what-alternate-do-we-have-to-manifest-file-for-running-offline-mobile-app – user2913184 Oct 27 '14 at 15:59