mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
Support http.getFile for URLs with query params
This commit is contained in:
@ -249,7 +249,7 @@ export var test_getContentAsFile = function (done) {
|
|||||||
|
|
||||||
// >> http-get-urlfile-content
|
// >> http-get-urlfile-content
|
||||||
var filePath = fs.path.join(fs.knownFolders.documents().path, "test.png");
|
var filePath = fs.path.join(fs.knownFolders.documents().path, "test.png");
|
||||||
http.getFile("https://httpbin.org/image/png", filePath).then(function (r) {
|
http.getFile("https://httpbin.org/image/png?testQuery=query&anotherParam=param", filePath).then(function (r) {
|
||||||
//// Argument (r) is File!
|
//// Argument (r) is File!
|
||||||
// >> (hide)
|
// >> (hide)
|
||||||
result = r;
|
result = r;
|
||||||
@ -457,7 +457,7 @@ export var test_request_responseContentToImageShouldReturnCorrectImage = functio
|
|||||||
export var test_request_responseContentToFileFromUrlShouldReturnCorrectFile = function (done) {
|
export var test_request_responseContentToFileFromUrlShouldReturnCorrectFile = function (done) {
|
||||||
var result;
|
var result;
|
||||||
|
|
||||||
http.request({ url: "https://raw.githubusercontent.com/NativeScript/NativeScript/master/apps/tests/logo.png", method: "GET" }).then(function (response) {
|
http.request({ url: "https://raw.githubusercontent.com/NativeScript/NativeScript/master/apps/app/ui-tests-app/image-view/red.png", method: "GET" }).then(function (response) {
|
||||||
result = response.content.toFile();
|
result = response.content.toFile();
|
||||||
try {
|
try {
|
||||||
TKUnit.assert(result instanceof fs.File, "Result from toFile() should be valid File object!");
|
TKUnit.assert(result instanceof fs.File, "Result from toFile() should be valid File object!");
|
||||||
@ -474,7 +474,7 @@ export var test_request_responseContentToFileFromUrlShouldReturnCorrectFile = fu
|
|||||||
export var test_request_responseContentToFileFromContentShouldReturnCorrectFile = function (done) {
|
export var test_request_responseContentToFileFromContentShouldReturnCorrectFile = function (done) {
|
||||||
var result;
|
var result;
|
||||||
|
|
||||||
http.request({ url: "https://httpbin.org/image/png", method: "GET" }).then(function (response) {
|
http.request({ url: "https://httpbin.org/image/png?queryString=param&another=anotherParam", method: "GET" }).then(function (response) {
|
||||||
result = response.content.toFile();
|
result = response.content.toFile();
|
||||||
try {
|
try {
|
||||||
TKUnit.assert(result instanceof fs.File, "Result from toFile() should be valid File object!");
|
TKUnit.assert(result instanceof fs.File, "Result from toFile() should be valid File object!");
|
||||||
|
17
tns-core-modules/http/http-request/http-request-common.ts
Normal file
17
tns-core-modules/http/http-request/http-request-common.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import * as fsModule from "../../file-system";
|
||||||
|
|
||||||
|
export function getFilenameFromUrl(url: string) {
|
||||||
|
const fs: typeof fsModule = require("file-system");
|
||||||
|
const slashPos = url.lastIndexOf('/') + 1;
|
||||||
|
const questionMarkPos = url.lastIndexOf('?');
|
||||||
|
|
||||||
|
let actualFileName: string;
|
||||||
|
if (questionMarkPos !== -1) {
|
||||||
|
actualFileName = url.substring(slashPos, questionMarkPos);
|
||||||
|
} else {
|
||||||
|
actualFileName = url.substring(slashPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = fs.path.join(fs.knownFolders.documents().path, actualFileName);
|
||||||
|
return result;
|
||||||
|
}
|
@ -4,6 +4,7 @@
|
|||||||
import * as imageSourceModule from "../../image-source";
|
import * as imageSourceModule from "../../image-source";
|
||||||
import * as platformModule from "../../platform";
|
import * as platformModule from "../../platform";
|
||||||
import * as fsModule from "../../file-system";
|
import * as fsModule from "../../file-system";
|
||||||
|
import { getFilenameFromUrl } from "./http-request-common";
|
||||||
|
|
||||||
// this is imported for definition purposes only
|
// this is imported for definition purposes only
|
||||||
import * as http from "../../http";
|
import * as http from "../../http";
|
||||||
@ -119,11 +120,11 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
toFile: (destinationFilePath?: string) => {
|
toFile: (destinationFilePath: string) => {
|
||||||
var fs: typeof fsModule = require("file-system");
|
var fs: typeof fsModule = require("file-system");
|
||||||
var fileName = callbacks.url;
|
|
||||||
if (!destinationFilePath) {
|
if (!destinationFilePath) {
|
||||||
destinationFilePath = fs.path.join(fs.knownFolders.documents().path, fileName.substring(fileName.lastIndexOf('/') + 1));
|
destinationFilePath = getFilenameFromUrl(callbacks.url);
|
||||||
}
|
}
|
||||||
var stream: java.io.FileOutputStream;
|
var stream: java.io.FileOutputStream;
|
||||||
try {
|
try {
|
||||||
|
@ -11,6 +11,7 @@ import * as utils from "../../utils/utils";
|
|||||||
import getter = utils.ios.getter;
|
import getter = utils.ios.getter;
|
||||||
|
|
||||||
import * as domainDebugger from "../../debugger/debugger";
|
import * as domainDebugger from "../../debugger/debugger";
|
||||||
|
import { getFilenameFromUrl } from "./http-request-common";
|
||||||
|
|
||||||
export enum HttpResponseEncoding {
|
export enum HttpResponseEncoding {
|
||||||
UTF8,
|
UTF8,
|
||||||
@ -148,11 +149,11 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
toFile: (destinationFilePath?: string) => {
|
toFile: (destinationFilePath?: string) => {
|
||||||
var fs: typeof fsModule = require("file-system");
|
var fs: typeof fsModule = require("file-system");
|
||||||
var fileName = options.url;
|
|
||||||
if (!destinationFilePath) {
|
if (!destinationFilePath) {
|
||||||
destinationFilePath = fs.path.join(fs.knownFolders.documents().path, fileName.substring(fileName.lastIndexOf('/') + 1));
|
destinationFilePath = getFilenameFromUrl(options.url);
|
||||||
}
|
}
|
||||||
if (data instanceof NSData) {
|
if (data instanceof NSData) {
|
||||||
data.writeToFileAtomically(destinationFilePath, true);
|
data.writeToFileAtomically(destinationFilePath, true);
|
||||||
|
@ -7,13 +7,13 @@ export function getString(arg: any): Promise<string> {
|
|||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
||||||
.then(r => {
|
.then(r => {
|
||||||
try {
|
try {
|
||||||
var str = r.content.toString();
|
var str = r.content.toString();
|
||||||
resolve(str);
|
resolve(str);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reject(e);
|
reject(e);
|
||||||
}
|
}
|
||||||
}, e => reject(e));
|
}, e => reject(e));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,13 +21,13 @@ export function getJSON<T>(arg: any): Promise<T> {
|
|||||||
return new Promise<T>((resolve, reject) => {
|
return new Promise<T>((resolve, reject) => {
|
||||||
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
||||||
.then(r => {
|
.then(r => {
|
||||||
try {
|
try {
|
||||||
var json = r.content.toJSON();
|
var json = r.content.toJSON();
|
||||||
resolve(json);
|
resolve(json);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reject(e);
|
reject(e);
|
||||||
}
|
}
|
||||||
}, e => reject(e));
|
}, e => reject(e));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user