Angular 6: Change Content-Type of template-generated page - excel

Notice: This question is not about ANGULAR's HttpClient, but the document generated by a component's template.
I want to do an old trick for a simple, yet powerful MS EXCEL export: Generate an HTML document containing one single <table>, and serve it with "Content-Type: application/xls". The user's browser will forward the document to a locally installed MS EXCEL, which detects that this is not a "real" .xls, but HTML, and converts very nicely the HTML into a spreadsheet.
My approach to implement this is straigtforward: Add a route for path, say, "/export-xls", and a new component that gets the data and renders it in its template.
However I absolutely cannot find a way to set the "Content-Type" header in the response!? I envisage that in the "Routes" array I would set "contentType: 'application/xls'", but no way.
What is the easiest and best way to generate such a document from within ANGULAR?

You can solve your problem by doing following steps
create a file containing your table in your local file system and create a URL for that file using URL.createObjectURL method
send an http request to that file
intercept the request and add contentType: 'application/xls' to response headers
after receiving your file revoke the URL using URL.revokeObjectURL method and delete the file from your local file system
I am not sure if you can do this without the creation of physical file because you need an http request because of the response content type

Related

Making a PATCH method request with a file to Common Data Service

I'm trying to create a Python script which can use the PATCH method to upload a file into MS's Common data service. I'm successfully making GET, POST, PATCH, and DELETE calls with simple data, but have so far been unable to configure it so that I can upload a file.
I've been using the Requests library for Python, with the requests.patch function in order to try updating the data. I'm attempting to upload a .csv file into the field, the file which i'm uploading has a filesize of 1kb.
If I upload the data directly into the common data service through the in-built data interface, my browser is able to correctly make a PATCH call. I've attempted to copy the call as closely as I can, but have had zero success.
File field in common data service
PATCH call in web browser
What is the correct way to make a PATCH request with a file to Microsoft's Common data service?
Made a mistake with the url in my request - I had missed out which field I was uploading data to
Incorrect URL:
https://90g9j3gf.crm4.dynamics.com/api/data/v9.0/test_entity(34cd854c-1175-4778-bf95-e1ce12dea3b0)
Corrected URL:
https://90g9j3gf.crm4.dynamics.com/api/data/v9.0/test_entity(34cd854c-1175-4778-bf95-e1ce12dea3b0)/test_field
The code I used to make the request:
Import requests
http_headers = {
'Authorization': 'Bearer ' + token['access_token'],
'Content-Type': 'application/octet-stream',
'x-ms-file-name': 'test.csv'
}
filedata = open("project-folder\\test.csv", "rb")
patch_req = requests.patch(
url, # My URL is defined elsewhere
headers=http_headers,
data=filedata
)
This now works correctly for me

instagram feed on website

I'm trying to display an Instagram feed on my website. I am not familiar with server side scripts. The explanations I have read go over my head.
I have managed to get an access key through Instagram but I don't know how to proceed.
I also tried the instructions from a video "How to implement an Instagram Feed in PHP without using the Instagram API"
For that, my php file works, displays the feed but I don't understand how to make it appear on my html page.
I'd be happy to make either method work.
In your HTML you should include a JavaScript 'script' element where you do something called an AJAX Request. The request will access the PHP file that produces the Instagram feed. ECHO the result from the PHP file and it will be returned into the DATA variable in the AJAX request. The request looks roughly like this:
$.ajax({
method: ‘POST or GET, most likely POST’,
url: ‘not always an actual url, this is the path to your PHP file’,
success: function(data) {
//php file returns into variable ‘data’
//display the feed within this function
}
});
The result from the PHP file should be echo’d back in JSON notation, if it isn’t, just add this to end of PHP the file:
$whatever = json_encode($your_result);
echo $whatever; //this is still the feed, just encoded in JSON
And if you need to decode it for some reason, just do
json_decode($variable_to_decode);
There are other parameters you can use in the AJAX request, like if for instance you need access to certain variables in your PHP file, that currently only exist in your HTML document.
this was solved by saving the html as php. i don't know why that is but it worked

Servicestack return wrong content type when returning a file with html extension

I am testing Servicestack Rest Files service. When calling it from angularjs and asking for an html file back, the results comes back as the standard servicestack html format instead of the json format. Even when appending ?format=json, it does not work correctly. I am trying to browse for html files and are then trying to load it into ace editor. I think Servicestack is getting confused with the response type. I checked and the Content-Type is set to application/json on the client side when doing the request.
I'm assuming the request you're making is:
GET /files/myfile.html
It's returing HTML because the .html file extension is a built-in registered format in ServiceStack so it assumes you're explicitly requesting the API in the HTML Format.
You can avoid this ambiguity by specifying the file Path on the QueryString, e.g:
GET /files?Path=myfile.html
Which tells ServiceStack to send it back using the most appropriate format as specified in the Accept header. Or if you prefer you can also explicitly specify the format by adding the {.ext} at the end of the path info, e.g:
GET /files.json?Path=myfile.html
Another option, if you're not using ServiceStack's built-in HTML support is to remove the HtmlFormat Plugin, e.g:
Plugins.RemoveAll(x => x is HtmlFormat);
Whicih will make .html no longer a registered format so the .html extension will be benign.
Note: removing HtmlFormat also removes built-in HTML functionality like its auto HTML pages

HTTP Error 405.0 - Method Not Allowed while Form Post

I posted a request to payu server via form submit using angularjs now once payment is completed payu will return a response with hash.But when it hits my success page i get "HTTP Error 405.0 - Method Not Allowed".I found many solutions online but none of that solved my issue.What i understood is that static html do not allow post by default.But my staticFile in IIS is like below
Request Path : *
Module : StaticFileModule
Name : staticFile
Request Restriction >Verb > All Verbs & Access > Script & Invoke > Files and folders
My question now in how to allow POST method for html page.I am using angular and if i change my success url to other than mine it works fine.I think there is some changes to be made to the web config but i tried my best but failed.Any help would be much appreciated.Also lets assume that the page successfully redirects to my success page how to capture the response that payu sends me online.
Thanks in advance if more input is needed from my side kindly ask in reply.
It's not that HTML does not allow POST by default, it's that HTML does not handle POST, period. (Not even if the HTML file contains JavaScript.) POST sends data to a script that runs on your server, and the script has to be smart enough to know what to do with the data. HTML isn't that smart. The only thing your server can do with HTML is to send the HTML back to whatever is requesting it. You need a server-side script that knows how to parse payu's response, do something appropriate with the hash, and then generate some HTML to display in the user's browser.

File not supported error while uploading profile image file to IBM Connections using REST API

I'm trying to upload a jpeg file for profile image using the Profiles REST API to IBM Connections_v5.0. I however get an error message "The type of the photo file you provided is not supported".
I'm however able to upload the same file directly using the Connections UI interface directly. I'm setting the MIME type correctly as "image/jpeg".
Also tried with GIF and PNG images but get the same error message.
Any pointers would be very helpful.
I'm just using FF restclient addon to fire a REST call. So basically doing a PUT on /profiles/photo.do?key=....
Content-Type is set as "image/jpeg" and the payload consists of the image data in binary (base 64) encoded.
The payload should just be the binary of the image, there is no need to Base64 encode it.
You should refer to Adding a Profile Photo
You need to use a Key (great stackoverflow post here)
If you know the key for a user's profile you can do the following:
key — This is generated by Connections itself during the population
process. It is used to define the users profile within the context of
Profiles and provides Connections with the ability to associate
content with a user when the users LDAP information has been altered.
It provides a separation of identity and helps facilitate user content
synchronization for Connections.
Once you have a key, you make the following request
URL: https://profiles.enterprise.example.com/profiles/photo.do?key=
b559403a-9r32-2c81-c99w-ppq8bb69442
METHOD: PUT
CONTENT-TYPE: image/png
input the binary content on the stream
you should be able to use "image/jpeg", "image/jpg", "image/png" or "image/gif"
If you have an error after the PUT method, you should add the SystemOut.log lines which are relevant.

Resources