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
|
||||
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!
|
||||
// >> (hide)
|
||||
result = r;
|
||||
@ -457,7 +457,7 @@ export var test_request_responseContentToImageShouldReturnCorrectImage = functio
|
||||
export var test_request_responseContentToFileFromUrlShouldReturnCorrectFile = function (done) {
|
||||
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();
|
||||
try {
|
||||
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) {
|
||||
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();
|
||||
try {
|
||||
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 platformModule from "../../platform";
|
||||
import * as fsModule from "../../file-system";
|
||||
import { getFilenameFromUrl } from "./http-request-common";
|
||||
|
||||
// this is imported for definition purposes only
|
||||
import * as http from "../../http";
|
||||
@ -119,11 +120,11 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
}
|
||||
});
|
||||
},
|
||||
toFile: (destinationFilePath?: string) => {
|
||||
var fs: typeof fsModule = require("file-system");
|
||||
var fileName = callbacks.url;
|
||||
toFile: (destinationFilePath: string) => {
|
||||
var fs: typeof fsModule = require("file-system");
|
||||
|
||||
if (!destinationFilePath) {
|
||||
destinationFilePath = fs.path.join(fs.knownFolders.documents().path, fileName.substring(fileName.lastIndexOf('/') + 1));
|
||||
destinationFilePath = getFilenameFromUrl(callbacks.url);
|
||||
}
|
||||
var stream: java.io.FileOutputStream;
|
||||
try {
|
||||
|
@ -11,6 +11,7 @@ import * as utils from "../../utils/utils";
|
||||
import getter = utils.ios.getter;
|
||||
|
||||
import * as domainDebugger from "../../debugger/debugger";
|
||||
import { getFilenameFromUrl } from "./http-request-common";
|
||||
|
||||
export enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
@ -150,9 +151,9 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
||||
},
|
||||
toFile: (destinationFilePath?: string) => {
|
||||
var fs: typeof fsModule = require("file-system");
|
||||
var fileName = options.url;
|
||||
|
||||
if (!destinationFilePath) {
|
||||
destinationFilePath = fs.path.join(fs.knownFolders.documents().path, fileName.substring(fileName.lastIndexOf('/') + 1));
|
||||
destinationFilePath = getFilenameFromUrl(options.url);
|
||||
}
|
||||
if (data instanceof NSData) {
|
||||
data.writeToFileAtomically(destinationFilePath, true);
|
||||
|
@ -7,13 +7,13 @@ export function getString(arg: any): Promise<string> {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
||||
.then(r => {
|
||||
try {
|
||||
var str = r.content.toString();
|
||||
resolve(str);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
}, e => reject(e));
|
||||
try {
|
||||
var str = r.content.toString();
|
||||
resolve(str);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
}, e => reject(e));
|
||||
});
|
||||
}
|
||||
|
||||
@ -21,13 +21,13 @@ export function getJSON<T>(arg: any): Promise<T> {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
||||
.then(r => {
|
||||
try {
|
||||
var json = r.content.toJSON();
|
||||
resolve(json);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
}, e => reject(e));
|
||||
try {
|
||||
var json = r.content.toJSON();
|
||||
resolve(json);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
}, e => reject(e));
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user