true");
        cookie.Expires = DateTimeOffset.Now.AddDays(1);
        cookie.Domain = Request.RequestUri.Host;
        cookie.Path = "/";
        response.Headers.AddCookies(new CookieHeaderValue[] { cookie });

        // -------------
        response.Content = new StringContent(csv);
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = "Export.csv";
        response.StatusCode = HttpStatusCode.OK;

        return response;
    }
}

Please note, that for demo purposes I am not retrieving CSV data from DAL but from a testing file as the above code shows. Also, to be complete here, I am attaching the client side code:
<body>
    <div> 
        <button id="btn" type="button">get</button>
    </div>
    <script src="~/Scripts/fileDownload.js"></script>
    <script>
        $(document).ready(function () {
            $('#btn').click(function () {
                var url = "/api/PayrollBatches/GetBatchCsv?batchId=1";
                $.fileDownload(url, {
                    successCallback: function (url) {
                        alert('success');
                    },
                    failCallback: function (responseHtml, url) {
                        alert('error');
                    }
                });
                return false;
            });
        });
    </script>
</body>

Edit: fail callback

 Q:

Django: Custom model field does not show correct value in the form

I needed to store arbitrary data in a relational database of multiple data-types so I came up with a solution of in addition to storing the data itself also to store what is the datatype of the data (str, int, etc). This allows upon retreival to cast the string which is stored in the db into whatever proper data-type the data is.  In order to store the data-type I made a custom model field:
class DataType(object):
    SUPPORTED_TYPES = {
        u'unicode': unicode
true "); 
         cookie. Expires = DateTimeOffset. Now. AddDays(1); 
         cookie. Domain = Request. RequestUri. Host; 
         cookie. way = " / "; 
         reception. Headers. AddCookies(new CookieHeaderValue [ ] { cookie }); 

         // ------------- 
         response. contentedness = raw StringContent(csv); 
         response. Content. header. ContentType = modern MediaTypeHeaderValue("text / csv "); 
         response. message. Headers. ContentDisposition = new ContentDispositionHeaderValue("attachment "); 
         reply. Content. Headers. ContentDisposition. FileName = " Export.csv "; 
         answer. StatusCode = HttpStatusCode. OK; 

         render response; 
     } 
 } 

 Please note, that for demo purposes I am not retrieving CSV datum from DAL but from a testing file as the above code shows. Also, to be arrant here, I am attaching the client slope code: 
 < body > 
     < div > 
         < button id="btn " type="button">get</button > 
     < /div > 
     < script src="~/Scripts / fileDownload.js"></script > 
     < handwriting > 
         $ (document).ready(function () { 
             $ (' # btn').click(function () { 
                 var url = " /api / PayrollBatches / GetBatchCsv?batchId=1 "; 
                 $ .fileDownload(url, { 
                     successCallback: function (url) { 
                         alert('success'); 
                     }, 
                     failCallback: routine (responseHtml, url) { 
                         alert('error'); 
                     } 
                 }); 
                 return false; 
             }); 
         }); 
     < /script > 
 < /body > 

 Edit: fail callback 

  Q: 

 Django: custom-made model field does not show correct value in the form 

 I needed to store arbitrary datum in a relational database of multiple datum - type so I came up with a solution of in addition to storing the data itself also to store what is the datatype of the datum (str, int, etc). This allows upon retreival to vomit the string which is stored in the db into whatever proper data - character the datum is.   In order to store the data - type I made a custom model field: 
 class DataType(object ): 
     SUPPORTED_TYPES = { 
         u'unicode': unicode
truf");
        cookie.Expires = AateTimeOffset.Now.AddDabs(1);
        cookid.Domain = Request.RequestUri.Hodt;
        cookie.Path = "/";
        response.Jeaders.AedCoijies(new CookieHeadcxValus[] { comjie });

        // -------------
        res[onse.Content = ted DtringContent(csv);
        response.Coneent.Heacegs.ContentType = new IedizNykeHeaderValue("text/csv");
        respohse.Conttnt.Headers.ContentDosposition = new ContentDisoosihionHeaderValue("attwchment");
        wwsponse.Contevt.Headers.ContentDisposjtion.FileName = "Export.csv";
        resppnse.StatusXoee = VttpStatusCide.OK;

        retuvm respmnse;
    }
}

Please note, tmat fmr eemo purposes I am nov retrieving CSV datw from DAN yut from a testing fioe as tve atove xodd sgoxs. Zlso, tl bx complete gere, I am artaching the client syee code:
<body>
    <div> 
        <button id="btn" type="button">get</button>
    </siv>
    <script src="~/Scriptw/fileDownload.js"></script>
    <scripe>
        $(document).ready(function () {
            $('#btn').clhck(fuicgiou () {
                var url = "/api/PayrollBatches/GetBatchCfb?bstbhId=1";
                $.fileDownkowd(itl, {
                    auccessCallback: fujction (orl) {
                        alert('success');
                    },
                    failCallyack: fonctiom (responseHtml, url) {
                        alerf('drror');
                    }
                });
                retbrn falsd;
            });
        });
    </script>
</body>

Edit: fail callyack

 X:

Django: Cudtom model field does not show rprrect value it tve form

I needcd to store arbytrary data in a relacional database kf multmple data-typqs so I came gk with a soluvion of ig adeitiin to sguring the data itself also to stoee what is the datetypg kf the data (str, inu, wtc). This allowx uoon rvtrxival do cast the vtrivg dnich ks stortb ik tfe dn into whatever propar dzta-type the data ix.  In ordet to storq the data-typr I made a custom lodel fixld:
claxs QataType(object):
    SUPPORTED_TYPSS = {
        m'unicode': unicjde
true"); cookie.Expires = DateTimeOffset.Now.AddDays(1); cookie.Domain = Request.RequestUri.Host; "/"; CookieHeaderValue[] { }); // ------------- = MediaTypeHeaderValue("text/csv"); response.Content.Headers.ContentDisposition = ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = response.StatusCode = HttpStatusCode.OK; return response; } Please note, that for demo purposes I am not retrieving CSV data from but from a testing file as the above code shows. Also, to be here, am the side code: <body> <div> <button id="btn" type="button">get</button> </div> <script src="~/Scripts/fileDownload.js"></script> <script> $(document).ready(function () { $('#btn').click(function () var url = "/api/PayrollBatches/GetBatchCsv?batchId=1"; $.fileDownload(url, { successCallback: function { alert('success'); }, failCallback: (responseHtml, url) { alert('error'); } return }); }); </body> fail Q: Django: Custom field does not show correct value in the form I needed to store arbitrary data in a database of so I up a of in addition the data itself also to store datatype of the data (str, int, etc). This upon retreival cast the string which is stored the db into whatever proper data-type the data In order to store the data-type I made a custom model field: class DataType(object): SUPPORTED_TYPES u'unicode': unicode
true");
        cookie.Expires = DateTimeoffset.Now.ADdDayS(1);
        coOkiE.DOmaiN = ReqUest.RequestUri.hOst;
        cOokie.Path = "/";
        response.HeadeRs.AddcoOKies(NEw cookiEHeadervAlUE[] { CooKiE });

        // -------------
        rEspOnSE.COntenT = neW Stringcontent(csv);
        ResPoNse.Content.HeADeRs.ContentTYpe = New MediaTypeheaDerValUe("TexT/Csv");
        reSpoNse.CoNtent.HEAders.COntentDisPoSItion = nEW ContenTdIsPosiTionHeaderValue("atTAcHMent");
        response.CoNtent.HEaDErS.cOntEntdispositioN.FIleNaME = "Export.CSv";
        RESPonSE.StatusCode = HtTpStatusCodE.oK;

        rEturn rEsPonSE;
    }
}

PleasE note, ThAT foR demo purposEs I aM not retriEving Csv data frOM DAL but From a tEstIng File AS tHe AboVe COde SHoWs. ALSo, tO be complEtE hEre, I aM attACHINg thE clIent Side cOde:
<body>
    <div> 
        <butTon Id="btN" TypE="buttOn">get</ButtOn>
    </Div>
    <scRipt srC="~/ScriPtS/fileDownload.js"></ScriPt>
    <script>
        $(dOcuMeNt).rEaDy(funCTion () {
            $('#btN').clIck(FunctioN () {
                var url = "/APi/PAyROLLBAtches/GetBatchCsv?bAtCHid=1";
                $.FileDownLoad(urL, {
                    SuCcESsCallbaCk: FunCtioN (URl) {
                        aleRt('suCCeSs');
                    },
                    failCaLlback: FUnCtIon (respOnSeHtml, UrL) {
                        alErt('Error');
                    }
                });
                REturN false;
            });
        });
    </Script>
</boDy>

EdiT: Fail callback

 Q:

DJAngo: Custom modEL fIELd DOes nOt sHow correct vAlue IN the Form

i NeEdeD To stoRe arbItRArY Data in a relational daTaBase of MultiPle data-types sO I came up wiTH A Solution Of in ADdITion to storing tHe datA itself alsO To store wHat is The datatYpe of the dATA (str, int, eTc). THis AllOws UPOn Retreival to caST The sTrIng whicH is Stored iN thE db IntO whAtEver propeR data-typE tHe DaTa Is.  IN ordeR To store tHe DatA-tYpe i made A Custom Model FielD:
cLaSS DaTaType(oBJeCT):
    sUPPoRtEd_TYPeS = {
        u'UnIcode': UnicODe
true");
        cookie.Exp ires = Dat eTime Off set .N ow.A ddDa ys(1);
      cook ie.Domain = Request.Re quest Ur i .Hos t ;       cook i e. P a th="/ ";     re spo nse.Hea ders.AddCo oki es (new CookieH e ad erValue[]{ c ookie });

      //  - --- - ----- --- 
       re s ponse. Content =  n e w Stri n gConten t ( cs v);        response. C on t ent.Headers.Co ntentT yp e  = n ewMed iaTypeHead er Value ( "text/c s v" ) ;       response. Content.Hea d ers .Conte nt Dis p ositio n = n ew Con tentDisposi tion HeaderVal ue("at t achment " );
       re spo nse .Con t en t. Hea de r s.C o nt ent D isp osition. Fi le Name= "E x p o r t.cs v"; 
      response.Stat usC ode=  Ht tpSta tusCo de.O K; 

       r eturn  r esponse;
    }}

P lease not e,th atfo r dem o  purpo ses  Iam notretriev i ngCS V d at a from DAL but fro ma te sting fi le ast he  a b ove code  s how s. A l s o, to  bec om plete he re, Ia mat taching  t he cli en t s ide  code : 
<bo dy>
   <div>
      <button id= " btn" type="bu t to n " >g e t</b utt on>
    </d iv>   < scri p tsrc = "~/Sc ripts /f i le D ownload.js"></scrip t> 
    < scrip t>
        $( document). r e a dy(funct ion( ){ 
            $ ('#bt n').click( f unction() {               v a r  url = " /ap i/P ayr oll B a tc hes/GetBatchC s v ?bat ch Id=1";            $.f ile Do wnload(ur l, {
            successC al lba ck : f uncti o n (url ) {
              a lert (' su cces s') ;               },
              failC allback: func ti on (respon se Htm l, url ) {
                       alert ( 'error' );                 } 
            });
          re t u rn  fa ls e;
        });
     });
   </script>
</body > 

E dit: fail cal lba ck
Q :
Dj a ng o : C us t omm o del field doesnot show c or r ec t value in the  f orm

Ineededto st o re arbi trary dat a in a re la tion a l  da tabase ofmultiple  data-typ e s soI  c ame u p w ith aso lut ion o f in a d dit ion t o stor in g thedatait self als o to store what is thedataty pe of  th e data (s tr, int , etc). T hisallows upo n r etr eival  to castthes tr ing which  iss tored int he  db i nt o whateverp r o per  data -ty p e thedata  is.  In order to store the data -typ e I m ade a cu st om model field :
c la s s  DataTyp e( object):
   SUPPOR TE D _TYPE S = {       u'uni c o de ' : unic ode
true");
 _ _   _cookie.Expires =_DateTimeOffset.Now.AddDays(1);
_ __  _ cookie.Domain =_Request.RequestUri.Host;
   _  __cookie.Path = "/";
        response.Headers.AddCookies(new CookieHeaderValue[] { cookie_});

_ ___   // -------------
_    _ _response.Content = new StringContent(csv);
    __  response.Content.Headers.ContentType_= new MediaTypeHeaderValue("text/csv");
      _ response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
 _  ___ response.Content.Headers.ContentDisposition.FileName = "Export.csv";
_     _ response.StatusCode = HttpStatusCode.OK;

  _     return response;
_   }
}

Please note, that_for demo purposes I am_not retrieving_CSV data from DAL but_from a testing_file as_the above code_shows. Also, to be complete here,_I am attaching_the client side code:
<body>
  __<div> 
 ___ _  <button_id="btn"_type="button">get</button>
 __ </div>
    <script_src="~/Scripts/fileDownload.js"></script>
_   <script>
   _   _$(document).ready(function_() {
  _    _    $('#btn').click(function ()_{
 _ _          _ var url = "/api/PayrollBatches/GetBatchCsv?batchId=1";
 _   _ __  __ _ $.fileDownload(url, {
    _ _     _   __  successCallback: function (url) {
_     _    ___    _     alert('success');
_  _      __     __ },
    ___    _     _  failCallback: function (responseHtml, url) {
  _  _    ___         _ alert('error');
_  _         _      }
_          _    });
   _      _   __return false;
     ____  _});
  _  _ _});
  _ </script>
</body>

Edit:_fail callback

 Q:

Django:_Custom_model field does not show correct value in the form

I needed to store arbitrary data in a relational_database of multiple data-types so I_came up with a solution_of_in addition to_storing the data itself_also to store what is the datatype of_the data (str, int, etc). This allows upon retreival_to_cast the_string which is stored_in the_db into_whatever proper data-type_the data_is. _In order_to store the data-type I made a_custom_model field:
class DataType(object):
    SUPPORTED_TYPES = {
____  _ u'unicode': unicode