I'd like to know how could I make a report downloadable from my AngularJS Application?
The report is a .xlsx I can post the data, but the response is:
What I'd like is a downloadable file, or to open the .xlsx on Excel Online in other tab as in the preview.
How could I do that?
I usually recommend to create a hidden form and do plain http submit to jsreport /api/report. This is the most stable way and works across all browsers.
<form method='POST' target='_blank' action='/api/report' id='jsrForm'>
<input hidden='true' name='template[shortid]' value="41ucBgXKe"/>
<input hidden='true' name='data[foo]' value="Hello world"/>
<input hidden='true' name='options[Content-Disposition]' value="attachment; filename=myreport.pdf"/>
</form>
<script>
document.getElementById("jsrForm").submit();
</script>
Do you have control over the response? If so, add the content-disposition header and MediaType header to the response:
For System.Net.Http.HttpResponseMessage
var response = new HttpResponseMessage{Content = ...........}
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
FileName = "mydoc.xlsx"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
For System.Net.WebClient
var client = new WebClient();
client.Headers.Add("Content-disposition", "attachment");
client.Headers.Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
Related
html
<form #form enctype="multipart/form-data">
<input type="file" #upload name='uploadingFiles' (change)="uploadFiles(form)" multiple>
</form>
Angular
void uploadFiles(form) {
var formData = new FormData(form);
final request = new HttpRequest();
request.open('POST', 'http://localhost:8080/uploadMulti');
request.upload.onProgress.listen((ProgressEvent e) {
print('progress');
});
request.onLoad.listen((e) {
print('Uploaded');
});
request.send(formData);
}
How can I put image manipulation code here to reduce an image that is 4000X4000 to 600X600 and then replace it in the formData before sending ? Is it possible or is the formData just meta-data passed to the server and then server uses that to pull image bytes one file at a time ?
Angular has a good package to make a compression on the image before you upload it to your server.
you can use this https://www.npmjs.com/package/ngx-image-compress
and here is a good article if you have doubts about who you can do it.
https://medium.com/swlh/compress-image-and-send-it-to-an-api-in-angular-bc48e6ed3835
I am trying to save an image in database but when I do the following it saves with dataURL, but that URL starts with localhost, how can I prevent it? I used React for frontend.
uploadImage = (event) => {
var imgSize = event.target.files[0].size;
$('#img').attr('hidden', false);
if (imgSize < 1000000 ) {
this.setState({
image: URL.createObjectURL(event.target.files[0]),
imageSize: imgSize
});
document.getElementById("errImgUpload").innerHTML = "";
}
else {
document.getElementById("errImgUpload").innerHTML = "Maximum image size 1Mb";
}
}
<div className="form-group">
<label for="file-upload" className="custom-file-upload">
<span className="fa fa-upload"></span> Upload image
</label>
<input onChange={(event) => this.uploadImage(event)} name="file-upload" id="file-upload" type="file" accept="image/*" />
<span id="errImgUpload" className="text text-danger"></span>
</div>
The Blob is http://localhost:10002/b46e96f5-83ce-4d10-b668-2bd038721b5a, what is a blob?
URL.createObjectURL() creates a blob which is a binary representation of the file in the memory. It doesn't upload the file. I am not sure from where you got the code. You may want to read more about this at MDN.
Uploading requires a backend service to open an endpoint for you to send post data to. You need to use <input type='file'> tag to send the file as form data and set the service endpoint as the url in the form. Start with this article.
You need to post your image data with FormData, like this:
const formData = new FormData();
formData.append('image', files[0]);
fetch(url, {
method: 'POST',
body: data
}).then(...);
And blob is Binary Large Object, more detail you can find in MDN.
<form
action="some url"
method="POST"
target="_blank"
>
{_.map(somedata, (value, key: string) => (
<input name={key} key={key} type="hidden" value={value}/>
))}
</form>
I am working with a page which submits a form on click of some button, this posts some data and opens a page in a new tab. This works perfectly fine in chrome mobile android but does not work(the new url opens in the new tab but shows no data posted by the form) when i create a PWA shortcut using Add to Home Screen feature and submit the form from inside of it. Also the new tab opens inside PWA only instead of mobile android chrome.
I apologise that this relates to Jquery (it uses core JS at its heart) but I believe this is something similar to solve the form posting issue. It uses the JS FormData object which allows for files as well.
function formPost(form){
var formData = new FormData();
$(form).find('input,select,textarea').each(function(){
if ($(this).attr('type') == 'file'){
use = $(this).get(0).files[0];
} else {
use = $(this).val();
}
formData.append($(this).attr('name'),use);
})
var request = new XMLHttpRequest();
request.open('POST',$(form).attr('action'));
request.send(formData);
}
Its worth saying that for browsers this requires at least IE10 but I think at end of 2019 we shouldn't be worrying about that !! https://caniuse.com/#search=formdata (PWAs operate in "modern browsers").
I have to store user profile photo in my firebase data base. I want to store a local image that has this path : "www/img/avatar.jpeg". I try this code to get image url
$scope.user.image="data:img/avatar.jpeg;base64"
But when running my code i get this message error : ERR_UNKNOWN_URL_SCHEME.
The user will have to select the photo, so you need a file selector in your HTML:
<input type="file" id="file" name="file" />
Then you can handle it in JavaScript with:
function handleFileSelect(e) {
var file = e.target.files[0];
// Get a reference to the location where we'll store our photos
var storageRef = storage.ref().child('chat_photos');
// Get a reference to store file at photos/<FILENAME>.jpg
var photoRef = storageRef.child(file.name);
// Upload file to Firebase Storage
var uploadTask = photoRef.put(file);
uploadTask.on('state_changed', null, null, function() {
// When the image has successfully uploaded, we get its download URL
var downloadUrl = uploadTask.snapshot.downloadURL;
// Set the download URL to the message box, so that the user can send it to the database
textInput.value = downloadUrl;
});
}
file.addEventListener('change', handleFileSelect, false);
This code comes from the Zero To App talk at Google I/O. Complete code is available in this gist.
Where it adds the download URL to a text box, you'll want to save it in the database.
I'm trying to upload a file to google drive from google app engine.
I have tried 2 different ways but I lose information in both.
The first one is the next one:
-The html form:
<html><body><form id='myForm' method='post' action='/guardar'
enctype='multipart/form-data' >
<input type="file" id="doc" name="doc" >
<input type="submit" value="Enviar"></form>
</body></html>
-The python code:
class guardar(webapp.RequestHandler):
#decorator.oauth_required
def post(self):
http = decorator.http()
service = build('drive', 'v2', http=http)
thefile = self.request.get('doc')
media_body = MediaInMemoryUpload(thefile, mimetype='text/plain', resumable=True)
response = service.files().insert(body={'title': 'prueba_si','mimeType': 'text/plain'},media_body=media_body).execute()
This way I lose the mimetype of the uploaded file and the title too; and I need both.
I have tried this other way but it always says that such file does not exist:
-The html file:
<html><body><form id='myForm' method='post' action='/guardar' >
<input type="file" id="doc" name="doc" >
<input type="submit" value="Enviar"></form>
</body></html>
-The python code:
class guardar(webapp.RequestHandler):
#decorator.oauth_required
def post(self):
http = decorator.http()
service = build('drive', 'v2', http=http)
thefile = self.request.get('doc')
mime_type=mimetypes.guess_type(thefile,strict=True)
media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)
response = service.files().insert(body={'title': 'prueba_si','mimeType': mime_type},media_body=media_body).execute()
Thanks a lot for the help!
You don't need to pass the mime type in the media upload and also in the metadata. I would leave it just in the media upload.
You should not lose the title information, but I cannot reproduce your error.