Why the browser can't show an EPPlus spreadsheet? - asp.net-mvc

I'm trying a lot of options with complete unsuccess. My need is create a spreadsheed in the server and the user gets it in the browser in a asp.net MVC 5 environment.
In the server the code is:
[HttpPost]
public ActionResult ExportToExcel([ModelBinder(typeof(DevExpressEditorsBinder))] Reports data)
{
ExcelPackage pck = new ExcelPackage();
var ws = pck.Workbook.Worksheets.Add("Sample1");
ws.Cells["A1"].Value = "Sample 1";
ws.Cells["A1"].Style.Font.Bold = true;
var shape = ws.Drawings.AddShape("Shape1", eShapeStyle.Rect);
shape.SetPosition(50, 200);
shape.SetSize(200, 100);
shape.Text = "Sample 1 text text text";
if (data.PDFFileName == null) data.PDFFileName = "Spreadsheet.xlsx";
data.PDFFileName = Path.GetFileNameWithoutExtension(data.PDFFileName) + ".xlsx";
FileContentResult result = new FileContentResult(pck.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
result.FileDownloadName = data.PDFFileName;
return result;
}
The view is
#Ajax.ActionLink("Created Spreadsheet", "ExportToExcel", "GraphReport", null, new AjaxOptions { HttpMethod = "POST" }, new { #class="btn btn-warning" })
The result in Chrome is:
Remote Address:[::1]:49711
Request URL:http://localhost:49711/GraphReport/ExportToExcel
Request Method:POST
Status Code:200 OK
Response Headers
Cache-Control:private, s-maxage=0
Content-Disposition:attachment; filename=Spreadsheet.xlsx
Content-Length:3561
Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Date:Mon, 29 Jun 2015 22:54:38 GMT
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:5.2
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcVXNlcnNcbWNhc3Ryb1xEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEzXFByb2plY3RzXE1jU29mdFxDVlNcQmVlaGl2ZU1WQ1xHcmFwaFJlcG9ydFxFeHBvcnRUb0V4Y2Vs?=
Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:31
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
The code is there in response:
PKÓÝFCjkh'¬[Content_Types].xmlµÛN!_eíYh½0ÆtÛjb}f»¤ÂÐþ½,[ij¢I{5?3ß?f½5Õ#iï6åV¡^i·jØûò©¾e%p
wØ°-æ³eª\ë¨a]JáNZ ´>ZHyW"\Ã
Why it is not being interpreted as download attached file?

It's because you cannot download file via AJAX call.
Solution: Create a form, use the POST method, submit the form. When the server page responds to the request, write a response header for the mime type of the file, and it will present a download dialog. You may need to use an iframe to prevent your site from navigating to error page if server returns an error.
Many of questions like this, they all have answers..
Ajax call to download file returned from RESTful service
Handle file download from ajax post

Related

C# reading OData $batch multipart/mixed response to a meaningful object(s)

I am consuming an OData Service, I am successfully POSTing my request (using RestSharp) to /$batch endpoint and getting the response. the response header contains
"Content-Type" : "multipart/mixed; boundary=<GUID>"
Body is
--C4254E82B51CFE5BD04201606B9AB7C50
Content-Type: multipart/mixed; boundary=C4254E82B51CFE5BD04201606B9AB7C51
Content-Length: 2221
--C4254E82B51CFE5BD04201606B9AB7C51
Content-Type: application/http; charset=utf-8
Content-Length: 2037
content-transfer-encoding: binary
HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 1732
location: https://test.api/Event/CarEntries('4003581738')
dataserviceversion: 2.0
etag: W/"datetimeoffset'2021-04-21T00%3A49%3A45Z'"
{ JSON }
--C4254E82B51CFE5BD04201606B9AB7C51--
--C4254E82B51CFE5BD04201606B9AB7C50
Content-Type: application/http; charset=utf-8
Content-Length: 15116
content-transfer-encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 15017
dataserviceversion: 2.0
{ JSON }
--C4254E82B51CFE5BD04201606B9AB7C50--
How do I deserialise and extract the JSON Objects in my C# code? I do not want to invent a Regex pattern (well that is my last resort)
I did try using "Simple.OData.Client" (also a few others) but my request is not 100% compatible with the "Simple.OData.Client".
Also tried extracting using the below code but not necessary give me what I want
var sc = new StringContent(response.Content);
var content = sc.ReadAsStreamAsync().Result;
var streamContent = new StreamContent(content);
streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse(response.ContentType);
var provider = streamContent.ReadAsMultipartAsync().Result;
Can someone giveme the best way to extract the Json objects ?
Thanks
Nero
I manage to get this working HttpClient and System.Net.Http.Formatting.Extension
Below is the code
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://service-url/Entity/v1.3/$batch"))
{
request.Headers.TryAddWithoutValidation("Accept", "application/json");
request.Headers.TryAddWithoutValidation("Client-Id", "XXXXXXXXX");
// Add all the headers here
// this is your custom batch request
request.Content = new StringContent("--batch\ncontent-type: multipart/mixed;boundary=changeset\n\n--changeset\ncontent-type: application/http\nContent-Transfer-Encoding: binary\n\nPOST CarEntries HTTP/1.1\ncontent-type: application/json;charset=utf-8\naccept: application/json;\n\n{\n\"RefId\": \"Test\",\n\"Child\": {\n\"ChildId\": \"412000415\"\n}\n}\n--changeset--\n--batch--");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/mixed;boundary=batch"); // This is imporatnt - but please refer to your api documentation
var response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
var multiPartContent = await response.Content.ReadAsMultipartAsync(); // This is part of the extension
var mixedContent = multiPartContent.Contents.First(); // you will have multiple contents, select the content you want
var data = await mixedContent.ReadAsStringAsync();// read it as string
Regex rg = new Regex(#"\{(.|\s)*\}"); // find the json object from mixed content
var json = rg.Match(data);
return JsonConvert.DeserializeObject<TMessage>(json.Value);
}
}
Hope this helps someone in the future. But still, my goal is to use "Simple.OData.Client" or "Microsoft.OData.Client"

How to view content delivered from HttpResponseMessage (ASP.NET MVC Web Api)

I create json object and assign it to a StringContent of my HttpResponseMessage instance. Everything works fine when I call the Web API action, the result is 200, the content-length is how it should be, but how to find the content itself, where is the json? What I get in the browser and in Postman is this:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: application/json
}
Why is this instead of my json string?
Content: System.Net.Http.StringContent
If what you are trying to achieve is to return a valid JSON response, then this is the way to go in Asp.Net MVC
public ActionResult HttpResponseMessage()
{
var oJSON = new { url = "path_to_file", hash = "aaaaaaaaaaaaaaaaa" };
return Json(oJSON, JsonRequestBehavior.AllowGet);
}
Response headers as seen by Postman:
Cache-Control →private
Content-Length →49
Content-Type →application/json; charset=utf-8
Date →Fri, 26 Oct 2018 13:31:44 GMT
Server →Microsoft-IIS/10.0
X-AspNet-Version →4.0.30319
X-AspNetMvc-Version →5.2
X-Powered-By →ASP.NET
X-SourceFiles →=?UTF-8?B?RTpcRXhhbSA3MCA0ODdcNzA0ODdcTVZDUm91dGVzXEhvbWVcSHR0cFJlc3BvbnNlTWVzc2FnZQ==?=
Response body as seen by Postman
{"url":"path_to_file","hash":"aaaaaaaaaaaaaaaaa"}

QuickBooks Authentication error when getting basic company information

I'm writing a simple desktop application to get information from QuickBooks(developer account using demo account(UK) data) and in this regard I've been able to go past the OAuth flow. However, I've not been able to get the basic company information
The below is a capture of the Fiddler request and response:
GET https://quickbooks.api.intuit.com/v3/company/123145829830639/companyInfo/123145829830639 HTTP/1.1
Authorization: oauth_token="****", oauth_nonce="z4x0a196", oauth_consumer_key="****", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1499283607", oauth_version="1.0", oauth_signature="EGw6Ty%2BKFAawrH1%2FSxQuFwaMcEo%3D"
Content-Type: application/json
Host: quickbooks.api.intuit.com
The generation of the header is similar to https://developer.intuit.com/v2/apiexplorer?apiname=V3QBO#?id=CompanyInfo but I end up getting the following response(Fiddler partial response) based on the request
intuit_tid: gw-c4e19f89-df78-42a5-ae7e-216187421143
Set-Cookie: JSESSIONID=21BF1FFEE48B39538E82485FD25C4280.c51-pprdsbxas901; Path=/; Secure; HttpOnly
QBO-Version: 1706.912
ErrorCode: 100
ErrorCause: AuthenticationErrorGeneral: SRV-110-Authentication Failure , statusCode: 401
Message: General Authentication Error
The code to access company information is as below:
string companyInfo = String.Format("company/{0}/companyInfo/{0}", authenticator.OAuthProfile.realmId);
string ciUrl = BASE_URL + companyInfo; //https://quickbooks.api.intuit.com/v3/
var sb = new System.Text.StringBuilder();
sb.AppendFormat("oauth_token=\"{0}\", oauth_nonce=\"{1}\", oauth_consumer_key=\"{2}\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"{3}\", oauth_version=\"1.0\", oauth_signature=\"{4}\"",
Manager.UrlEncode(_token),
Manager.UrlEncode(_nonce),
Manager.UrlEncode(_consumer_key),
Manager.UrlEncode(_timestamp),
Manager.UrlEncode(_signature));
var authorisationHeader = sb.ToString().TrimEnd(' ').TrimEnd(',');
// Request Company Information
var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(ciUrl);
request.Headers.Add("Authorization", authorisationHeader);
request.Method = "GET";
request.ContentType = "application/json";
using (var response = (System.Net.HttpWebResponse)request.GetResponse())
{
// get 401
}
Used sandbox URL and added minorversion to the base URL
I attempted setting BASE_URL set to the sandbox url(https://sandbox-quickbooks.api.intuit.com/v3) and also set minorversion to be 4.
Any help is much appreciated.
Hopefully this helps, but here is a Java snippet using their SDK that works for me:
OAuthAuthorizer oauth = new OAuthAuthorizer(System.env.QB_OAUTH_CONSUMER_KEY, System.env.QB_OAUTH_CONSUMER_SECRET,
vendor.intuitOAuthAccessToken, vendor.intuitOAuthAccessSecret);
UUID trackingID = UUID.randomUUID()
log.info("About to init Context companyID=" + vendor.realmId + ", app_token=" + System.env.QB_APP_TOKEN + ", uuid=" + trackingID.toString())
Context context = new Context(oauth, System.env.QB_APP_TOKEN, ServiceType.QBO, vendor.realmId)
context.setMinorVersion("4")
context.setTrackingID(trackingID)
log.info("About to set BaseURL")
Config.setProperty(Config.BASE_URL_QBO, System.env.QB_BASE_URL + "/v3/company");
log.info("About to init DataService")
// get all customers
log.info("About to executeQuery")
DataService service = new DataService(context)
QueryResult queryResult = service.executeQuery("select * from customer");
In my case, QB_BASE_URL=https://sandbox-quickbooks.api.intuit.com
*NOTE there isn't a trailing slash
vendor.intuitOAuthAccessToken and vendor.intuitOAuthAccessSecret are the values you get back after the oauth flow

Accessing Online Slim API from iphone & IOS application

I have simple API developed in Slim, which is stored in Online Server. It works fine when I check it from browser, but when I retrieve it from Iphone app, it show me the following error:
PeopleAlsoAsk[12607:2219389] Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 500" UserInfo={NSLocalizedRecoverySuggestion="this is index pagpe, Specific questions are retrieved successfully", NSErrorFailingURLKey=http://upvc.pk/test2/public/, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x608000001060> { URL: http://upvc.pk/test2/public/ }, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x60800003d6c0> { URL: http://upvc.pk/test2/public/ } { status code: 500, headers {
"Accept-Ranges" = bytes;
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Length" = 81;
Date = "Wed, 26 Apr 2017 12:34:31 GMT";
Server = LiteSpeed;
Vary = "Accept-Encoding";
"X-Powered-By" = "PHP/7.0.17";
} }, NSLocalizedDescription=Expected status code in (200-299), got 500}
2017-04-26 17:34:31.462 PeopleAlsoAsk[12607:2219389] Error function called
I tried a lot, but all in vain. If any one faced this problem, or know about this, then please guide me in this. Thanks in advance.
My code is given below
public/index.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$app = new \Slim\App;
//Questions Routes
require '../src/routes/questions.php';
$app->run();
routes/questions.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
//Get All Questions
$app->get('/api/questions', function(Request $request, Response $response){
$questions = "All questions are retrieved successfully";
echo json_encode($questions);
});
// Get specific Questions
$app->get('/api/questions/{app_id}', function(Request $request, Response $response){
$questions = "Specific questions are retrieved successfully";
echo json_encode($questions);
});
You should put your iOS code here.
From your Slim, I would place:
$app->status($status_code);
In your response code, you can add this function to give all client response:
function echoResponse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
Usage very simple:
echoResponse(200, "your response");
From iOS point of view, hard to say something without the code.

Why GetResponseStream does not execute Cookies on server response on data GET from MVC Razor View?

I have a .net website on IIS that has an virtual directory pointing to MVC application. I am trying to reuse a sitemaster.master on the RAzor view header.
I have this code below on a Razor view _hearder_it.cshtml.
I am doing a StreamReader on test.aspx page which has a sitemaster.master only. The req.GetResponse does return the stream from the sitemaster(menu bar etc.). However the sitemaster.master has Request.Cookies and the cookies never have a value. I know they should have a value because I already test outside of the mvc application. The cookie changes the view of the sitemaster and that is the reason I need it.
//This code does returns the stream .
WebRequest req = HttpWebRequest.Create(url );
req.Method = "GET";
string source;
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
source = reader.ReadToEnd();
Response.Write(source); // I get HTML result back from my sitemaster.master.
Cookies are sent in request headers, while you don't add any cookies to your webrequest here. Here is a post that might help you
I added the cookie in the CookieContainer. This code is working successfully.
This code is in Razor view _header_it.cshtml:
#{
string userTyp3 = Request.Cookies["MY_USERTYPE"] != null ? Server.UrlDecode(Request.Cookies["MY_USERTYPE"].Value) : "";
CookieCollection _CookieCollection2 = new CookieCollection();
HttpWebRequest _request2 = (HttpWebRequest)WebRequest.Create("http://MySite_TEST/it/test.aspx");
_request2.Method = "GET";
_request2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
_request2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)";
_request2.Referer = "http://MySite_TEST/it";
_request2.KeepAlive = true;
//Here is auth cookie, works fine
_request2.CookieContainer = _cookieContainer;
_request2.CookieContainer.Add(( new Cookie ( "MY_USERTYPE", userTyp3 , "/", "MySite_TEST") )) ;
_request2.Headers.Add(HttpRequestHeader.CacheControl, "no-cache=set-cookie");
HttpWebResponse _response2 = (HttpWebResponse)_request2.GetResponse();
StreamReader _reader2 = new StreamReader(_response2.GetResponseStream());
Response.Write(_reader2.ReadToEnd()); //
_response2.Close();
_reader2.Close();
}
I used the example on this URL:
http://stackoverflow.com/questions/2476092/login-website-curious-cookie-problem?rq=1]
Thank you

Resources