How i download/display a file(pdf/ppt/docx) in flutter app? - android

im getting a file from a repository in the web, and i want to download or if possible without downloading display/open it (in case of display be open remotely), if i have to download it, it do it as well xD, but my question is, the body i cant to nothing possible, so i have to check content-type form header on the response and probably the bodyBytes??
var reply;
var url = '$host/repositoryStream/$id?BACOSESS=$bacosess';
var response = await http.get(url);
if ( response.statusCode == HttpStatus.OK)
reply = response;
what do i do with the bytes or the body to open it in the phone??

for those who still don't know there is a plugin very handy, it works for me.
open_file you still have to request/check permissions with simple_permissions.
i hope this would help the guys who have been waiting for this.

Related

How to get file size using Url before download in flutter [duplicate]

How to get file size from URL (in Flutter)? I am able to get it by using:
http.Response response = await http.get(url);
print(response.contentLength);
But that downloads the entire file. Is it possible to get the file size without entirely downloading it? Thanks
Found the answer: HEAD Request.
http.Response r = await http.head(url);
r.headers["content-length"]
Note: r.contentLength; directly doesn't work.
I tried the above-accepted answer for firebase storage file download size before download the file. But I see that http.head doesn't contain content-length. So I tried http.get method and I saw the content-length is contained in the header.So
This should be the correct answer I think.
http.Response r = await http.get(url);
final file_size = r.headers["content-length"];

Superagent .attach throws error code 500

I want to upload a picture from phone (android) to a server (Editing avatar of a profile).
To select the picture i am using react-native-image-picker.
This is my API-Call
request
.post(apiurl)
.set("auth", token)
.attach('avatar', avatar)
.end(async function (err,res) {
if(res.status === 200) {
console.log("Update views...");
}
});
As far as is read and understood the docs, i just need to pass the filepath to the .attach function.
So mine looks like this (android):
avatar = file:///storage/emulated/0/path/on/phone/new_avatar.jpg
I get the path from the react-native-image-picker and add the file:// at the beginning.
But what am I missing?
In the server logs we can see that it returns 500. In our API-Test class, at least as far as we tested it, it only returns 500, when it couldnĀ“t find the file, e.g. a wrong path was passed as an argument.
Cheers and thanks in advance!

How can a json file read in Android using Ionic and AngularJS Frameworks?

I have a problem now regarding about the json that can't be read in my android..
the json file is where my data is place..it act as an static database..
I can get it with my Desktop but when it come to my mobile it didn't show..
Here is my sample code:
Here is my Services to get my json file..
var serviceUrl = '/';
$http.get(serviceUrl + 'JSON/Books.json').success(function (results) {
$scope.New = results;
});
Please help me to solve this problem.. my idea about the problem is the serviceUrl. Any idea about it. Thank you so much..
Im definitely a beginner for this Ionic Framework.
To all who still in this problem I just find something that solve it. I don't know if it will solve in your problem but it really solve in me.. I use file:///android_asset/www/ as my serviceUrl
So this is an example:
var serviceUrl = 'file:///android_asset/www/';
$http.get(serviceUrl + 'JSON/Books.json').success(function (results) {
$scope.New = results;
});
Just try it and explore to it.. i just tried that one and it worked in me..
Maybe the reason is the all the json file in your apk installer will be placed in file:///android_asset/www/json directory in android phone so let your url point to that directory..
I hope it will help you and explore in it..that might not be the correct answer but i hope it will help you find the hint.
Thank you
Starting the serviceUrl with a '/' makes it an absolute URL. It will work in chrome since the root is the www folder. But in cordova mobile environment it will translate to file:///.
Simply add a '.' ('./') to make it a relative path and it will work in both android and ios environments.
another easy way is to turn your json file into a javascript file, (for static files if you want to update use something like pouchdb).
eg save the json document as myjsondata.js
var mynamespace = mynamespace || {};
mynamespace.data = [{"foo":"bar"}];
then reference the javascript file in you main page where you load all the other js files
<script src="js/myjsondata.js"></script>
then in your service you can just access the json with mynamespave.data
Based on #Datz Me answer, I've found it's way easier if you treat your application as a web server (which it is) and request your file as being served by it instead of trying to figure out how to manage the different file paths between the several builds.
Here is what I did.
I've placed my json file on
www/json/app.json
Inside it I've put
{
"title": "Application Title",
"icon" : "custom-icon.png"
}
And in my app controller I used the following code to read the properties:
$http.get('json/app.json').success(function (results) {
$rootScope.title = results.title;
$rootScope.icon = results.icon;
});
And in all my child controllers, I just need to add $rootScope as a dependency and I'm able to use
{{title}} //on headings
{{icon}} //to display the image path
<img src="{{icon}}"/> //to display the icon
In my case, it's an app that will be customised for several clients, so I needed a way to quickly change the app's properties and keep them in one place.

How to get Hyperlink from whole string

I create an application in which i get the response from web service .
The response is
"I might be in danger. I have triggered my panic alarm which is connected to you. Call me now. If i'm not answering, contact the police. My position is:http://maps.google.com/maps?q=21.183783,72.823548"
3.I store the string in text view.and i want to open HTTP URL in browser,on the click of text.but how can i get HTTP URL in whole string plese give me idea.
You can do this easily with php...
If you are able to run php, this should do it.
$string = $_GET['string'];
OR
$string = $_POST['string'];
this may change depending on how you get the responce from the website, feel free to send me the form which you get the responce and ill change it accordingly.
$string_chunks = explode('http://',$string,2);
$url = 'http://'.$string_chunk['1'];
Basically, this will take the string, find the "http://" and create 2 strings out of it. one with the content before the "http://" and one with the content after, which is the url. so it would return $string_chunk['0'] and $string_chunk['1']
var response = "ur response string";
var indexofHttp = response.indexOf('http://');
var url = response.substring(indexofHttp);

Setting a request body in Android's DownloadManager?

For the next step of my application, I need to add download functionality. The user chooses what they want to download and could select anything from 1 file to thousands of them if they could be bothered to select that many.
I want to use Android's built in DownloadManager to provide this downloading functionality, but unfortunately I cannot see how I could implement it for my scenario.
In order for the target server to authorize the download, I need to send some JSON along in the request body. Like this, if I was doing it manually:
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
output.writeBytes(rawData);
output.flush();
Where rawData is the JSON string. The request body is always set to POST.
I can't seem to find any way to add this JSON string to the DownloadManager, and until I can do that, the server will always reject the download.
The only other solution that I can think of, which I desperately want to avoid, is writing a PHP script on my server to take some GET parameters, generate the JSON and then redirect the request.
Does anybody know of a way that I can send my JSON data along with the DownloadManager? Each file that I'm downloading needs its own, unique, JSON string.
you cannot do that in Android's Download manager, see Download Manager Issue,
I had a similar requirement and I ended up Using HttpClient (Xamarin).
Sample Code-
using(var httpClient = new HttpClient()) {
using(var request = new HttpRequestMessage(new HttpMethod("POST"), URL)) {
request.Headers.TryAddWithoutValidation("User-Agent", userAgent);
request.Headers.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
request.Headers.TryAddWithoutValidation("Accept-Language", "en-US,en;q=0.5");
request.Headers.TryAddWithoutValidation("Connection", "keep-alive");
request.Headers.TryAddWithoutValidation("Referer",RefererURL);
request.Content = new StringContent("YOUR_REQUEST_BODY_HERE");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var responser = await httpClient.SendAsync(request);
return responser;
}

Categories

Resources