如何在Nativescript中将图像上传到PHP服务器?

时间:2016-11-08 06:06:07

标签: javascript php image server nativescript

我试了这么久,没有效果。 我有一个Nativescript和服务器PHP的应用程序。 我正在尝试

function sendImages(fileUri) {
var request = {
    url: "http://path/to/my/server",
    method: "POST",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        "ENCTYPE":"multipart/form-data",
        "Content-Disposition": "form-data",
        "File-Name": fileUri,
    },
    description: "{uploading:"+fileUri+"}"
};
var task = session.uploadFile(fileUri, request);
return task;
}

function sendImages(fileUri){
    var locationRequest = 'http://path/to/my/server';
    var request = new XMLHttpRequest();
    var fs = require("file-system");
    var path = fs.path.normalize(fileUri);
    var file = fs.File.fromPath(path);
    file.readText().then(function(content){
        request.open("POST",locationRequest,true);
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        request.onload = function(data){
            console.log('success');
        };
        request.onerror = function(data){
            console.log(this.responseText);
            console.log(data);
        };

        request.send('fileUri='+content);
    }, function(error){
        alert('error: '+error);
    });
}

我在$_POST$_REQUEST数组中找到了结果:

array(11) {
 ["яШяб"]=>
  string(0) ""
  ["'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzѓ„…†‡€‰Љ’“”•–—™љўЈ¤Ґ¦§Ё©ЄІіґµ¶·ё№єВГДЕЖЗИЙКТУФХЦЧШЩЪбвгдежзийкстуфхцчшщъяД"]=>
  string(0) ""
  ["'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ѓ„…†‡€‰Љ’“”•–—™љўЈ¤Ґ¦§Ё©ЄІіґµ¶·ё№єВГДЕЖЗИЙКТУФХЦЧШЩЪвгдежзийктуфхцчшщъяЪ"]=>
  string(0) ""
  ["бПyUљVчЎI4е~ьЦґ№\Zoд
_лЫЏя"]=>
  string(0) ""
  ["гJMКK^ј­їv*Ri?›hўЉgдБEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEP_J~О±‡‡х
GсgгЭпЩtЖ·ыMѕущдаџ6y8*¤rq‚OjтЩПАцџю8x{ВЊBKiпјЛЁЫЈЕґ®§ШЄшЧµБDю)ji«iџt«¦ЉНmыRHО¬Y–$oe_2КzЁВwШэsГЬ›'Бd8о,О(*ф°Ь°§MЫ–udЧД›ХGљ-¦¤њdЭ›ЌџAuыFюВЮи~шom}gk\iю…•±юЬЕYыуО}jн‡Ѓbя"]=>
  string(169) "hеHкҐгЦ®йж8zр®ЙРtТ‚ЉkHоУKH№sҐї ±їсЇаПЉѕшЪ_ш—lЁWН±ѕ‰HЋкH3Рс‚ЅЏЁБ;їІOВk?‹я lґmfЬK¦iс5юЈt’4 ,gЩќђэТХпЯµН–•с‹цOУ>0ЫЪЄ\ЩЕi9(“нЋXіиФџъж ђя ‚iЕl|Mв№9lmU}ҐдЭъ…э)"
  ["Ъ;kр6CCЕ,і‚|Ш,Z§€‚’m{7Н/fщµj\–WЧ–IJн6ПЫЧгЦЁъпь(П   ЯµЅ•¤(ъСЃ¶™¤a№`8юR¤Ћ„°оЧМµШ~Р7—_ь]-щ>`свьЗ8U™ХGэтqфв¬Џ€с77ЗgsЏћ*Wцu'J__Fе(Е$Ы¶ЬОЦNR”­vВѕ‚эѓѕ:jѕсдхЛч“HЦK‘І-nЂ$mфWБR?јTъззЪи~НymсkГ3йьОћ І0Њхo"]=>
  string(317) "p)џ5ГщЦ/‡sЄ–ыф¤¤µjйomydЇwM®§{ыk|(Ішaсћk­ФE§k°эєЮ4X¤,DЁ=б» p;WђЧХ?рSЎай‚Џ0ќAIцgя щZ”v>ПЕьв6>ќтЕК3·ч§N3“щКMьфР(ўЉgжЎEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEP¤юИ:х—‡?i/ЯЯИ9.¤¶З<РЙгОЈс®лю
'б
KLші§шИДЖПTТ–$—°–"
  ["`ЛнтІДъWПцЧwЭЪLСЛ‡ЋDl2°9Ды'б‡Ж/„_µзГTшUс‰ ѓ]UPСЙ_‰§•AqlЗшъе"]=>
  string(346) "ИБ^Ёэ‡ГмГ.ОxgБшЪсўс5(NzAUЌЇJъsЁЕGOжJтqЊѕ3§G“H°Г;і ЄЈ$“Р_Jш—ю   µвшх7я „;в6ќ5™l§ц”2G"ЏC°0cпЗРWgр§цFш]ы:°ш©сѓЖЦ··y[Йp‚ [W!ЂbLІqтфзўдEР°ЮсЗЧy1Р…<[s­*”Ь#Э№ЩKљЦWIЁфжqWiЯ¬ЗВїШ6ЧАъбЯKa§ЩyDя ЛЗ’ИїЂI?*сџШKв§Ѓю:Е¤кs€нµыF°Зf,#х,»»Х/ЪГцЋЏ,ЉЛAY!рю”МєzH0ЧxiШvИ (<Ѓћ„‘^QУ[L—тґrFБ‘С€*AИ Ћ†„ґ0гЋ/АSг<|ЌуQЛЎF•)6Ъ©м[•Э№]›|®ПЮK™4Ґeнї·oВќCБ_"
  ["сЕµ«fx€    Ј•WеK…P%B}Nып>†јBѕ»ш9ыIь,эЎ|я"]=>
  string(0) ""
  ["WAШy‘†хЪґ“¶Њъ$аXшЃЉџp„ЈUVjUh9Ж5)T•Ь›SiZRMьZ¶щ9ЎЄщ‚ЅЏц!шSЁ|@шСiвimЏцg‡o_f_БgЙ@Ѕјnъ!ц®лБїрMЅyп’_€­"¶RГЈВТ;Ћа<ЃB}v·Тєя"]=>
  string(0) ""
  ["ќЅе¬љM$“sЏ–БA|kвЏЊЮУо‘x~ЗЛ©Иќо?ѓон^
Sj:ЌюЇЁП«j—rOss3Kq<­–‘Ш’МOrI"]=>
  string(0) ""
  ["Ў¦•‘щqn_в<NkV<®¬®—h¤ЈЯVўў›конЁQEПњ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
(ўЂ
_д}(ўЂ:­#г§ЖЅПы?GшЇЇБ"]=>
  string(0) ""
}

如果我将“Content-Type”值更改为“application / octet-stream”或“multipart / form-data”或其他,我有空$_POST$_GET,{{1我服务器上的{}和$_FILES数组。 同样,我如何在我的服务器PHP上传图像?

1 个答案:

答案 0 :(得分:0)

我解决了将图片转换为base64的过程,就像字符串一样。你必须注意替换字符编码,但它的工作原理。