Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not upload txt file #46

Closed
Zengratul opened this issue Dec 9, 2016 · 10 comments
Closed

Can not upload txt file #46

Zengratul opened this issue Dec 9, 2016 · 10 comments
Labels

Comments

@Zengratul
Copy link

Zengratul commented Dec 9, 2016

I tested with image and pdf file, it's really good. I try to upload a text.txt file with body "AAA", 3 bytes. It happens error as below:

2016-12-09_10-48-22
2016-12-09_10-50-42

It may upload text file as text/html, not text/plain. In Postman, it works well. Something happens, please help.

@tsonevn
Copy link
Contributor

tsonevn commented Dec 9, 2016

Hi @Zengratul,
Thank you for your interest in nativescript-background-http,

I tested the given scenario, while uploading txt file with this plugin, however was unable to reproduce this behavior. It could be some problem with the REST API, which you are using to upload the file. Could you verify, whether this is not something related with the REST API. It would also help, if you could provide sample app, which could be debugged locally.

Regards,
@tsonevn

@Zengratul
Copy link
Author

Zengratul commented Dec 12, 2016

Dear @tsonevn,
Thank you for your reply, I send you the example app and Postman API for your test. It works well with Postman. The file I uploaded is Text.txt, and body is "AAA". The upload file must be binary.

You can test with pdf, image files. It works well. Please change directory as my example if your device is not rooted.

Upload Test.zip

Upload Test.apk.zip

Thank you,

@tsonevn
Copy link
Contributor

tsonevn commented Dec 12, 2016

Hi @Zengratul,
Thank you for the sample project.
I reviewed your project, and made some changes in user.service, while debugging to ber able to get the file from the default app folder from the project. I followed the below-described steps.

  1. Create folder named files with Text.txt file in app folder.
  2. Change the code to get the path to the app folder using knownFolders.currentApp().path.
  3. After building the app on Android device, tap Create Key, Create Location and Upload File buttons.
  4. As a result I receive a message with following information {"desc":"","secure":0,"name":"Text.txt","size":4}

I am attaching the code from user.service with my changes.
user.service.ts

import { Injectable } from "@angular/core";
import { Http, Headers, Response, RequestOptions } from "@angular/http";
import { Observable } from "rxjs/Rx";
import "rxjs/add/operator/do";
import "rxjs/add/operator/map";
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import * as bghttp from 'nativescript-background-http';
import { File, Folder, knownFolders, path } from 'file-system';

import { Config } from "../config";

@Injectable()
export class UserService{

  uploadkey: any = [];
  location: any="";

  constructor(private http: Http) {}

  createUploadKey(){let headers = new Headers(); 
    headers.append("Content-Type", "application/json");
    alert("You need set Text.txt file here:" + path.join(knownFolders.currentApp().path, "files/Text.txt"))
    /*alert(path.join(knownFolders.documents().parent.parent.parent.parent.path, "Text.txt"))*/
    //Or you can set Text.txt file in root directory.
    return this.http.post(Config.api + "v2/up-keys", { headers: headers })
    .map(res => {this.uploadkey = res.json(); console.log(this.uploadkey.key) }).catch(this.handleErrors);}

  getUploadLocation(){let headers = new Headers(); 
    console.log("upload ")
    headers.append("Content-Type", "application/json");
    var that =this;
    return this.http.put(Config.api + "v2/up-keys/" + that.uploadkey.key + "/upload", JSON.stringify({
        file_name: "Text.txt",
        file_size: 4
      }), { headers: headers }
    ).map(res => res.json())
    .do(data=>{
      console.log("-----------------data------------------------")
      console.log(JSON.stringify(data));
      that.location = data.location;
    })
    .catch(this.handleErrors);
  }

  uploadFile(){
    console.log(this.location);
    var that = this;
    let session = bghttp.session("file-upload"); 
    let request = {
        url: that.location,
        method: "POST",
        headers: {"Content-Type": "application/octet-stream"},
        description: "Upload file"
      };
      /*alert(path.join(knownFolders.documents().parent.parent.parent.parent.path, "Text.txt"))*/
    let task = session.uploadFile(path.join(knownFolders.currentApp().path, "files/Text.txt"), request);
    task.on("complete", uploadComplete);
    function uploadComplete(e){alert(e.response.getBodyAsString()); console.log((e.response.getBodyAsString()));}
  }

  handleErrors(error: Response) {
    console.log(JSON.stringify(error.json()));
    return Observable.throw(error);
  }
}




Could you verify whether you will have the same problem, while using the edited app. It would help if you could also give us more info about the result, which has been expected.

Let me know, whether this helps or if I could assist you further.

@Zengratul
Copy link
Author

Zengratul commented Dec 13, 2016

Dear @tsonevn,
I am looking forward to receiving this response as you: {"desc":"","secure":0,"name":"Text.txt","size":4}

I installed one time again Java SDK and test again but It doesn't worked.

I have tested your code on both my Galaxy Tab S2, Galaxy Note 4 and on Genymotion. I placed a file on /data/data/org.nativescript.Test/files/app/files/Text.txt as your code. However, I only receive below error.

On my device:
An uncaught Exception occurred on "main" thread.
com.tns.NativeScriptException:
Calling js method onClick failed

[object Object]
File: "/data/data/org.nativescript.Test/files/app/tns_modules/@angular/core/bundles/core.umd.js, line: 9464, column: 20

StackTrace:
Frame: function:'DebugAppView._rethrowWithContext', file:'/data/data/org.nativescript.Test/files/app/tns_modules/@angular/core/bundles/core.umd.js', line: 9464, column: 21
Frame: function:'', file:'/data/data/org.nativescript.Test/files/app/tns_modules/@angular/core/bundles/core.umd.js', line: 9477, column: 27
Frame: function:'', file:'/data/data/org.nativescript.Test/files/app/tns_modules/nativescript-angular/renderer.js', line: 219, column: 26
Frame: function:'ZoneDelegate.invoke', file:'/data/data/org.nativescript.Test/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js', line: 190, column: 28
Frame: function:'onInvoke', file:'/data/data/org.nativescript.Test/files/app/tns_modules/@angular/core/bundles/core.umd.js', line: 5975, column: 41
Frame: function:'ZoneDelegate.invoke', file:'/data/data/org.nativescript.Test/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js', line: 189, column: 34
Frame: function:'Zone.run', file:'/data/data/org.nativescript.Test/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js', line: 83, column: 43
Frame: function:'NgZone.run', file:'/data/data/org.nativescript.Test/files/app/tns_modules/@angular/core/bundles/core.umd.js', line: 5865, column: 66
Frame: function:'zonedCallback', file:'/data/data/org.nativescript.Test/files/app/tns_modules/nativescript-angular/renderer.js', line: 218, column: 24
Frame: function:'Observable.notify', file:'/data/data/org.nativescript.Test/files/app/tns_modules/data/observable/observable.js', line: 149, column: 23
Frame: function:'Observable._emit', file:'/data/data/org.nativescript.Test/files/app/tns_modules/data/observable/observable.js', line: 168, column: 18
Frame: function:'onClick', file:'/data/data/org.nativescript.Test/files/app/tns_modules/ui/button/button.js', line: 33, column: 32

at com.tns.Runtime.callJSMethodNative(Native Method)
at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1022)
at com.tns.Runtime.callJSMethodImpl(Runtime.java:907)
at com.tns.Runtime.callJSMethod(Runtime.java:895)
at com.tns.Runtime.callJSMethod(Runtime.java:879)
at com.tns.Runtime.callJSMethod(Runtime.java:871)
at com.tns.gen.android.view.View_OnClickListener.onClick(android.view.View$OnClickListener.java)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

On console log:
2016-12-13_09-54-59

And on logcat:
E/SELinux (20770): [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
I/art (20770): Late-enabling -Xcheck:jni
D/TimaKeyStoreProvider(20770): TimaSignature is unavailable
D/ActivityThread(20770): Added TimaKeyStore provider
D/ResourcesManager(20770): creating new AssetManager and set to /data/app/org.nativescript.Test-1/base.apk
I/TNS.Native(20770): NativeScript Runtime Version 2.4.1, commit b6676b88f846e551e0e20e67e9bc09d60dfb3089
D/TNS.Native(20770): JNI_ONLoad
D/TNS.Native(20770): JNI_ONLoad END
D/TNS.Native(20770): Failed to load snapshot: dlopen failed: library "libsnapshot.so" not found
D/TNS.Native(20770): V8 version 5.2.361
D/TNS.Native(20770): lenNodes=66504, lenNames=774254, lenValues=888077
D/TNS.Native(20770): time=7
D/TNS.Java(20770): Enabling NativeScript Debugger Agent
D/Activity(20770): performCreate Call secproduct feature valuefalse
D/Activity(20770): performCreate Call debug elastic valuetrue
W/art (20770): Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
V/JS (20770): Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
D/OpenGLRenderer(20770): Render dirty regions requested: true
I/Adreno (20770): EGLInit: QTI Build: 03/02/15, 210b328, I0829b9e471, LA.BF.2.1.05.00.00.203.165
I/OpenGLRenderer(20770): Initialized EGL, version 1.4
I/Adreno (20770): GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
I/Adreno (20770): GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
I/Adreno (20770): GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
I/Adreno (20770): GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
I/Adreno (20770): GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
I/Adreno (20770): GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
I/Adreno (20770): GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
I/Adreno (20770): GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
D/OpenGLRenderer(20770): Get maximum texture size. GL_MAX_TEXTURE_SIZE is 16384
D/OpenGLRenderer(20770): Enabling debug mode 0
I/Timeline(20770): Timeline: Activity_idle id: android.os.BinderProxy@21eb1c28 time:457934111
D/ViewRootImpl(20770): ViewPostImeInputStage ACTION_DOWN
I/System.out(20770): (HTTPLog)-Static: isSBSettingEnabled false
I/System.out(20770): (HTTPLog)-Static: isShipBuild true
I/System.out(20770): (HTTPLog)-Thread-234950-305498230: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
I/System.out(20770): (HTTPLog)-Static: isSBSettingEnabled false
I/System.out(20770): KnoxVpnUidStorageknoxVpnSupported API value returned is false
D/ViewRootImpl(20770): ViewPostImeInputStage ACTION_DOWN
E/ViewRootImpl(20770): sendUserActionEvent() mView == null
D/ViewRootImpl(20770): ViewPostImeInputStage ACTION_DOWN
V/JS (20770): upload
I/System.out(20770): (HTTPLog)-Static: isSBSettingEnabled false
I/System.out(20770): KnoxVpnUidStorageknoxVpnSupported API value returned is false
D/skia (20770): --- SkImageDecoder::Factory returned null
V/JS (20770): S-Ktki_LeLZ2j-CDMV0Yj4ue8G3cItkk
D/skia (20770): --- SkImageDecoder::Factory returned null
V/JS (20770): -----------------data------------------------
V/JS (20770): {"key":["Key is invalid."]}
D/ViewRootImpl(20770): ViewPostImeInputStage ACTION_DOWN
E/ViewRootImpl(20770): sendUserActionEvent() mView == null
D/ViewRootImpl(20770): ViewPostImeInputStage ACTION_DOWN
E/ViewRootImpl(20770): sendUserActionEvent() mView == null
D/ViewRootImpl(20770): ViewPostImeInputStage ACTION_DOWN
V/JS (20770): undefined
E/art (20770): invalid stream - problem with parameter iterator in /data/app/org.nativescript.Test-1/base.apk for method void com.tns.gen.android.view.View_OnClickListener.onClick(android.view.View)
D/AndroidRuntime(20770): Shutting down VM
E/art (20770): invalid stream - problem with parameter iterator in /data/app/org.nativescript.Test-1/base.apk for method void com.tns.gen.android.view.View_OnClickListener.onClick(android.view.View)
W/System.err(20770): com.tns.NativeScriptException:
W/System.err(20770): Calling js method onClick failed
W/System.err(20770): [object Object]
W/System.err(20770): File: "/data/data/org.nativescript.Test/files/app/tns_modules/@angular/core/bundles/core.umd.js, line: 9464, column: 20
W/System.err(20770): StackTrace:
W/System.err(20770): Frame: function:'DebugAppView._rethrowWithContext', file:'/data/data/org.nativescript.Test/files/app/tns_modules/@angular/core/bundles/core.umd.js', line: 9464, column: 21
W/System.err(20770): Frame: function:'', file:'/data/data/org.nativescript.Test/files/app/tns_modules/@angular/core/bundles/core.umd.js', line: 9477, column: 27
W/System.err(20770): Frame: function:'', file:'/data/data/org.nativescript.Test/files/app/tns_modules/nativescript-angular/renderer.js', line: 219, column: 26
W/System.err(20770): Frame: function:'ZoneDelegate.invoke', file:'/data/data/org.nativescript.Test/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js', line: 190, column: 28
W/System.err(20770): Frame: function:'onInvoke', file:'/data/data/org.nativescript.Test/files/app/tns_modules/@angular/core/bundles/core.umd.js', line: 5975, column: 41
W/System.err(20770): Frame: function:'ZoneDelegate.invoke', file:'/data/data/org.nativescript.Test/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js', line: 189, column: 34
W/System.err(20770): Frame: function:'Zone.run', file:'/data/data/org.nativescript.Test/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js', line: 83, column: 43
W/System.err(20770): Frame: function:'NgZone.run', file:'/data/data/org.nativescript.Test/files/app/tns_modules/@angular/core/bundles/core.umd.js', line: 5865, column: 66
W/System.err(20770): Frame: function:'zonedCallback', file:'/data/data/org.nativescript.Test/files/app/tns_modules/nativescript-angular/renderer.js', line: 218, column: 24
W/System.err(20770): Frame: function:'Observable.notify', file:'/data/data/org.nativescript.Test/files/app/tns_modules/data/observable/observable.js', line: 149, column: 23
W/System.err(20770): Frame: function:'Observable._emit', file:'/data/data/org.nativescript.Test/files/app/tns_modules/data/observable/observable.js', line: 168, column: 18
W/System.err(20770): Frame: function:'onClick', file:'/data/data/org.nativescript.Test/files/app/tns_modules/ui/button/button.js', line: 33, column: 32
W/System.err(20770): at com.tns.Runtime.callJSMethodNative(Native Method)
W/System.err(20770): at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1022)
W/System.err(20770): at com.tns.Runtime.callJSMethodImpl(Runtime.java:907)
W/System.err(20770): at com.tns.Runtime.callJSMethod(Runtime.java:895)
W/System.err(20770): at com.tns.Runtime.callJSMethod(Runtime.java:879)
W/System.err(20770): at com.tns.Runtime.callJSMethod(Runtime.java:871)
W/System.err(20770): at com.tns.gen.android.view.View_OnClickListener.onClick(android.view.View$OnClickListener.java)
W/System.err(20770): at android.view.View.performClick(View.java:5191)
W/System.err(20770): at android.view.View$PerformClick.run(View.java:20916)
W/System.err(20770): at android.os.Handler.handleCallback(Handler.java:739)
W/System.err(20770): at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err(20770): at android.os.Looper.loop(Looper.java:145)
W/System.err(20770): at android.app.ActivityThread.main(ActivityThread.java:5944)
W/System.err(20770): at java.lang.reflect.Method.invoke(Native Method)
W/System.err(20770): at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err(20770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
W/System.err(20770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
I/Process (20770): Sending signal. PID: 20770 SIG: 9

Thank you,

@tsonevn
Copy link
Contributor

tsonevn commented Dec 13, 2016

Hi @Zengratul,
I tested the project on Samsung Galaxy S4 and it seems to work as expected. Could you try to delete node_modules and platforms folder from your project and clean npm cache using npm cache clear. Also make sure that you have add Text.txt file in <project_name>/app/files folder. Then try to rebuild the project using tns run android
Archive.zip.
If this does not help, could you try to build the attached project, which I use for testing.

Let me know, whether this helps.

@Zengratul
Copy link
Author

Zengratul commented Dec 15, 2016

Dear @tsonevn,
I tested your code which you sent me. And I tried, realized that if you had placed Text.txt file inside app before you built code, it would work.

However, image or pdf file, you can build first and add file later, it works well. I tried rename file name of image into Text.txt -> It works well.

Please check build code first -> add file Text.txt later -> it will appear errors. And do with image or pdf, it will works, you can set file anywhere to upload, it also works. I think the problem is from body of file and encode binary, not extension of file.

Thank you for your reply,

@tsonevn
Copy link
Contributor

tsonevn commented Dec 16, 2016

Hi @Zengratul,
This case could be also be caused due to some Android platform specifics. If you first build the app and then add the file, most probably the app will not be notified for the new file, which could lead to crash. To resolve this behavior you could try the following scenario. After you add the Text.txt file, kill the app and then start it again.

Regarding to the topic, changing files inside the app folder manually on the device is not the best practice. You could use file-system modules to save and read files from device documents or temp directories.

Regards,
@tsonevn

@Zengratul
Copy link
Author

Thank @tsonevn for your help,

I think the problem: var task = session.uploadFile("file/path/bigpig.jpg", request);

The code uploads run before the code select file, it causes error. I try to separate a new variable to select file, and it works well.

Sincerely,

@tsonevn
Copy link
Contributor

tsonevn commented Dec 20, 2016

Hi @Zengratul,

Indeed the best way is to check, whether the file is available and then to start to uploading the file.
Do you need some further help or I could close this issue?

Regards,
@tsonevn

@Zengratul
Copy link
Author

Zengratul commented Dec 20, 2016

Thank you @tsonevn, you can close this #46 issue.

If you can help me the #45 issue, I am really grateful to you. I need to build an multi language app. And I can not change " upload completed successfully " into another language.

Faithfully,

@tsonevn tsonevn closed this as completed Dec 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants