c# - JsonRequestBehavior Error When downloading file from WebAPI -


a similar question asked here: getting error while download file in mvc2 has 1 answer doesn't solve problem , doubt solved problem other person...

here's problem: trying expose documents whether pdf, xlsx, tif, or type through webapi , here's current code on api:

public httpresponsemessage download(string documentid, string contactid)     {         var path = documentsource + "\\";         var document = documentdomain.getdocument(new guid(contactid), new guid(documentid));          if (referenceequals(document, null) && referenceequals(document.filename, null))         {             return new httpresponsemessage(httpstatuscode.notfound);         }         else         {              try             {                 memorystream responsestream = new memorystream();                 stream filestream = new filestream(path.combine(path,                 document.portfolioid == null ? "" : document.portfolioid.tostring(), document.filename), filemode.open);                 bool fullcontent = true;                 if (this.request.headers.range != null)                 {                     fullcontent = false;                      rangeitemheadervalue range = this.request.headers.range.ranges.first();                       // specified, seek requested position.                     if (range.from != null)                     {                         filestream.seek(range.from.value, seekorigin.begin);                          // in case, complete file returned.                         if (range.from == 0 && (range.to == null || range.to >= filestream.length))                         {                             filestream.copyto(responsestream);                             fullcontent = true;                         }                     }                     if (range.to != null)                     {                         // 10-20, return range.                         if (range.from != null)                         {                             long? rangelength = range.to - range.from;                             int length = (int)math.min(rangelength.value, filestream.length - range.from.value);                             byte[] buffer = new byte[length];                             filestream.read(buffer, 0, length);                             responsestream.write(buffer, 0, length);                         }                         // -20, return bytes beginning specified value.                         else                         {                             int length = (int)math.min(range.to.value, filestream.length);                             byte[] buffer = new byte[length];                             filestream.read(buffer, 0, length);                             responsestream.write(buffer, 0, length);                         }                     }                     // no range.to                     else                     {                         // 10-, return specified value end of file.                         if (range.from != null)                         {                             if (range.from < filestream.length)                             {                                 int length = (int)(filestream.length - range.from.value);                                 byte[] buffer = new byte[length];                                 filestream.read(buffer, 0, length);                                 responsestream.write(buffer, 0, length);                             }                         }                     }                 }                 // no range header. return complete file.                 else                 {                     filestream.copyto(responsestream);                 }                 filestream.close();                 responsestream.position = 0;                  httpresponsemessage response = new httpresponsemessage();                 response.statuscode = fullcontent ? httpstatuscode.ok : httpstatuscode.partialcontent;                 response.content = new streamcontent(responsestream);                 return response;             }             catch (ioexception)             {                 throw new httpresponseexception( httpstatuscode.internalservererror);             }         }     } 

i have separate mvc application, , have following code in controller , tries download specified documents:

public actionresult downloaddocument(string documentid, string documentname)     {         var currentmember = members.getcurrentmember();         if (currentmember != null)         {             var contactid = currentmember.getproperty("contactid").value.tostring();             try             {                 httpwebrequest req = (httpwebrequest)webrequest.create(configurationmanager.appsettings["apibaseurl"] + "/api/document/downloadexcel?documentid=" + documentid + "&contactid=" + contactid);                 //req.method = "get";                 var response = (httpwebresponse)req.getresponse();                  byte[] data;                 using (stream s = response.getresponsestream())                 {                     data = streamtobytearray(s);                                         }                   var mimetype = system.web.mimemapping.getmimemapping(documentname);                 response.appendheader("content-disposition", "inline; filename=" + documentname);                  return file(data, mimetype);              }             catch (exception ex)             {                 return json(new { message = ex.tostring() });             }         }          return null;     } 

the following error getting, , positive has nothing jsonrequestbehaviour suggested, since not returning json api:

this request has been blocked because sensitive information be
disclosed third party web sites when used in request. allow requests, set jsonrequestbehavior allowget.

stack trace:

[invalidoperationexception: request has been blocked because sensitive information disclosed third party web sites when 

this used in request. allow requests, set jsonrequestbehavior allowget.]
system.web.mvc.jsonresult.executeresult(controllercontext context) +292 system.web.mvc.controlleractioninvoker.invokeactionresult(controllercontext controllercontext, actionresult actionresult) +39
system.web.mvc.controlleractioninvoker.invokeactionresultfilterrecursive(ilist1 filters, int32 filterindex, resultexecutingcontext precontext, controllercontext controllercontext, actionresult actionresult) +116
system.web.mvc.controlleractioninvoker.invokeactionresultfilterrecursive(ilist
1 filters, int32 filterindex, resultexecutingcontext precontext, controllercontext controllercontext, actionresult actionresult) +529
system.web.mvc.controlleractioninvoker.invokeactionresultfilterrecursive(ilist1 filters, int32 filterindex, resultexecutingcontext precontext, controllercontext controllercontext, actionresult actionresult) +529
system.web.mvc.controlleractioninvoker.invokeactionresultfilterrecursive(ilist
1 filters, int32 filterindex, resultexecutingcontext precontext, controllercontext controllercontext, actionresult actionresult) +529
system.web.mvc.controlleractioninvoker.invokeactionresultfilterrecursive(ilist1 filters, int32 filterindex, resultexecutingcontext precontext, controllercontext controllercontext, actionresult actionresult) +529
system.web.mvc.controlleractioninvoker.invokeactionresultwithfilters(controllercontext controllercontext, ilist
1 filters, actionresult actionresult) +106
system.web.mvc.async.<>c__displayclass2b.b__1c() +321 system.web.mvc.async.<>c__displayclass21.b__1e(iasyncresult asyncresult) +185
system.web.mvc.async.wrappedasyncresult1.callenddelegate(iasyncresult asyncresult) +42
system.web.mvc.async.wrappedasyncresultbase
1.end() +133
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +56
system.web.mvc.async.asynccontrolleractioninvoker.endinvokeaction(iasyncresult asyncresult) +40
system.web.mvc.controller.b__1d(iasyncresult asyncresult, executecorestate innerstate) +34
system.web.mvc.async.wrappedasyncvoid1.callenddelegate(iasyncresult asyncresult) +70
system.web.mvc.async.wrappedasyncresultbase
1.end() +133
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +56
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +37
system.web.mvc.controller.endexecutecore(iasyncresult asyncresult) +44 system.web.mvc.controller.b__15(iasyncresult asyncresult, controller controller) +39
system.web.mvc.async.wrappedasyncvoid1.callenddelegate(iasyncresult asyncresult) +62
system.web.mvc.async.wrappedasyncresultbase
1.end() +133
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +56
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +37 system.web.mvc.controller.endexecute(iasyncresult asyncresult) +39
system.web.mvc.controller.system.web.mvc.async.iasynccontroller.endexecute(iasyncresult asyncresult) +39
system.web.mvc.mvchandler.b__5(iasyncresult asyncresult, processrequeststate innerstate) +39
system.web.mvc.async.wrappedasyncvoid1.callenddelegate(iasyncresult asyncresult) +70
system.web.mvc.async.wrappedasyncresultbase
1.end() +133
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +56
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +37
system.web.mvc.mvchandler.endprocessrequest(iasyncresult asyncresult) +40 system.web.mvc.mvchandler.system.web.ihttpasynchandler.endprocessrequest(iasyncresult result) +38
system.web.callhandlerexecutionstep.system.web.httpapplication.iexecutionstep.execute() +9765121 system.web.httpapplication.executestep(iexecutionstep step, boolean& completedsynchronously) +155

you returning json in catch section of try catch block in mvc application @ downloaddocument action. why getting error.

try

return json(new { message = ex.tostring() }, jsonrequestbehavior.allowget); 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -