javascript - Download File From MVC -


i need way pass list of strings when user clicks icon angular/script , send mvc controller in .net. list needs supposed download file in browser. have learned cannot via ajax and/or pretty messy.

edit: list of strings refers list of file ids retrieving, zipping 1 file, downloaded. not want store zipped file anywhere permanently.

i open ideas!

        $http.post('document/downloadfiles', data).success(function ()         {/*success callback*/ });      [httppost]     public actionresult downloadfiles(list<string> fileuniqueidentifiers)     {            var file =    _service.archiveanddownloaddocuments(fileuniqueidentifiers);          file.position = 0;          return new filestreamresult(file, "application/force-download");       } 

ok, third-time lucky guess? (i think this'll last implementation sorry jim - you'll have work rest out yourself, think i've given more enough free pointers now... if want more can contact me , i'll charge write it! :p).

this version uses cookie-based interchange, accepts input strings (assuming filenames) javascript, stores these in instance class along token key, assembles zipfile in-memory (without writing disk), , returns zipfile content result. efficiency remove actual token checks against guid list , check against key in file list. won't want filenames hard-coded in javascript i've done, can work part out yourself. hint: create database table identifier/filepath pairs , use identifiers lookup individual file paths after request sent server...

view part (i added mine index.cshtml):

<script type="text/javascript">     function sendstringandgetfiles() {          var files = ['c:\\temp\\afile.txt', 'c:\\temp\\afile2.txt', 'c:\\temp\\afile3.txt'];          $.ajax({             type: "post",             url: "/home/getfile",             contenttype: 'application/json',             data: json.stringify(files),             success: function (result) {                 //alert("yes worked! - " + result);                 window.location = "/home/getfile";             }         });     } </script>  <h5>just click</h5> <button onclick="sendstringandgetfiles()">send string , files</button> 

then controller part (i used homecontroller.cs)

    [acceptverbs(httpverbs.post)]     public string getfile(string[] strings)     {         guid token = guid.newguid();          inmemoryinstances instance = inmemoryinstances.instance;          instance.addtoken(token.tostring());          instance.addfiles(token.tostring(), strings);          httpcookie cookie = new httpcookie("cookietoken");          cookie.value = token.tostring();          this.controllercontext.httpcontext.response.cookies.add(cookie);          return token.tostring();     }      [acceptverbs(httpverbs.get)]     public actionresult getfile()     {         inmemoryinstances instance = inmemoryinstances.instance;          if (this.controllercontext.httpcontext.request.cookies.allkeys.contains("cookietoken"))         {             httpcookie cookie = this.controllercontext.httpcontext.request.cookies["cookietoken"];              if (instance.checktoken(cookie.value))              {                                                 cookie.expires = datetime.now.adddays(-1);                  this.controllercontext.httpcontext.response.cookies.add(cookie);                  memorystream ms = new memorystream();                  string[] filenames = instance.getfiles(cookie.value);                  using (ziparchive zs = new ziparchive(ms,ziparchivemode.create, true))                 {                     (int i=0;i < filenames.length; i++)                         zs.createentryfromfile(filenames[i], path.getfilename(filenames[i]));                                         }                  filecontentresult resultcontent = new filecontentresult(ms.toarray(),"application/zip");                  instance.removefiles(cookie.value);                  resultcontent.filedownloadname = "arandomlygeneratedfilenamehere.zip";                  return resultcontent;             } else              {                 return view("index");             }         }                     else         {             return view("index");         }      } 

inmemoryinstances class:

public class inmemoryinstances {     private static volatile inmemoryinstances instance;     private static object syncroot = new object();      private list<guid> activetokens;     private namevaluecollection filesbykeycollection;      private inmemoryinstances()      {         activetokens = new list<guid>();         filesbykeycollection = new namevaluecollection();     }      public static inmemoryinstances instance     {                  {             if (instance == null)              {                 lock (syncroot)                                   {                     if (instance == null)                          instance = new inmemoryinstances();                 }             }              return instance;         }     }      public bool checktoken(string token)     {         return activetokens.contains(new guid(token));     }      public string[] getfiles(string token)     {         return filesbykeycollection.getvalues(token);     }      public bool addfiles(string token, string[] files)     {         (int = 0; < files.length; i++)             filesbykeycollection.add(token, files[i]);          return true;     }      public bool addtoken(string token)     {         activetokens.add(new guid(token));         return true;     }      public bool removefiles(string token)     {         filesbykeycollection.remove(token);         return true;     }      public bool removetoken(string token)     {         return activetokens.remove(new guid(token));     } } 

hope helps!


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

gradle error "Cannot convert the provided notation to a File or URI" -