Answered by:
how do i handle two type of response using unbtrusive ajax

Question
-
User-1045082127 posted
hi there
in our scenario the action returned two type of response in some condition s one of them may be partial view and another one can be file .
how to handle boths using unobtrusive ajax (if file return to download then show partial view)
Wednesday, September 4, 2019 6:29 AM
Answers
-
User-1045082127 posted
the best way is <g class="gr_ gr_18 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="18" data-gr-id="18">reponse</g> a <g class="gr_ gr_91 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="91" data-gr-id="91">jsons</g> data that <g class="gr_ gr_110 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="110" data-gr-id="110">is contains</g> the response type and if <g class="gr_ gr_153 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="153" data-gr-id="153">response</g> type is okay then <g class="gr_ gr_192 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="192" data-gr-id="192">link</g> of <g class="gr_ gr_240 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="240" data-gr-id="240">file</g> (image) to show on end client browser.
so
return Json(new{MsgType="Success",Msg="files/Image/regnrtgiurn.jpg"});
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 8, 2019 6:03 PM
All replies
-
User753101303 posted
Hi,
Using jQuery ? My first thought would be to check the response header: https://stackoverflow.com/questions/1557602/jquery-and-ajax-response-header for the text/html content type to show or trigger the download dialog. This last point can be done from Ajax using a trick such as https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request
I would still double check if it brings a real benefit and would try to make sure a simpler design can't fit my need.
Edit: ah or you meant you want to show a message "after" the download. I'm not sure it's really easy. The common pattern is likely to show a message when the download is triggered else it would be rather #2 above and then you do whatever you want after that. Note that it prevents moving away from the page while the browser handles the download...
Wednesday, September 4, 2019 7:16 AM -
User-474980206 posted
unobtrusive ajax only supports html/text response. on sucessful response, it set an inner div to the response. if the response is a binary file, then it will display it as bad unicode data.
the samples in the links download the file twice. once in ajax, and then again with the browser (location = url).
while you can use ajax to fetch a file, you can only save it to local storage, or if image encoded as a data url, display it as an image or canvas. but you will need to write your own ajax, not use unobtrusive.
Wednesday, September 4, 2019 2:12 PM -
User-1045082127 posted
the best way is <g class="gr_ gr_18 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="18" data-gr-id="18">reponse</g> a <g class="gr_ gr_91 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="91" data-gr-id="91">jsons</g> data that <g class="gr_ gr_110 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="110" data-gr-id="110">is contains</g> the response type and if <g class="gr_ gr_153 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="153" data-gr-id="153">response</g> type is okay then <g class="gr_ gr_192 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="192" data-gr-id="192">link</g> of <g class="gr_ gr_240 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="240" data-gr-id="240">file</g> (image) to show on end client browser.
so
return Json(new{MsgType="Success",Msg="files/Image/regnrtgiurn.jpg"});
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 8, 2019 6:03 PM