JavaScript code can be fully deducted from the official phonegap website.
The phonegap Version 2.3 is used.
<SCRIPT type = "text/JavaScript" src = "cordova-2.3.0.js"> </SCRIPT> <SCRIPT type = "text/JavaScript"> var picturesource; var destinationtype; document. addeventlistener ("deviceready", ondeviceready, false); function ondeviceready () {picturesource = navigator. camera. picturesourcetype; destinationtype = navigator. camera. destinationtype;} function onphotodatasuccess (imagedata ){//????????? Base64 ???????????? // Console. Log (imagedata );//???????? VaR smallimage = Document. getelementbyid ('smallimage ');//?????????????? Smallimage. style. Display = 'block ';//??????????? //?????? CSS ???????????? Alert ('0: '); smallimage. src = imagedata; alert ('1: '); // upload, upload, and upload try {var Options = new fileuploadoptions (); // used to set parameters, the request string options on the server. filekey = "fileaddpic"; alert (imagedata); options. filename = imagedata. substr (imagedata. lastindexof ('/') + 1); alert ('1: '+ options. filename); // if the image format is used, use image/JPEG. For other file formats, check the API options on the official website. mimetype = "image/JPEG"; // The URI here is set according to your own needs. It is the address of alert ('2: ') for receiving and uploading images :'); VaR uri = encodeuri ("http: // 192.168.1.71: 803/up. aspx "); options. chunkedmode = false; alert (3); var Ft = new filetransfer (); alert (4); ft. upload (imagedata, Uri, function (MSG) {var response = MSG. response; alert ('OK' + response) ;}, function (MSG) {var response = MSG. response; alert ('no' + response) ;}, options); alert ('00');} catch (EE) {alert (EE. message );}}//?????? Parameters ????????? Uri ?????? Function onphotourisuccess (imageuri ){//???????????????? Uri // console. Log (imageuri );//???????? VaR largeimage = Document. getelementbyid ('largeimage ');//?????????????? Largeimage. style. Display = 'block ';//??????????? //?????? CSS ???????????? Largeimage. src = imageuri ;}//?? Capture photo ????????? Parameters ????????? Function capturephoto (){//???? ?????????????????? Base64 ????????????????? Navigator. Camera. getpicture (onphotodatasuccess, onfail, {quality: 50, destinationtype: Camera. destinationtype. file_uri });}//?? Capture editable photo ????????? Parameters ????????? Function capturephotoedit (){//???? ?????????????????? Base64 ??????????????????? Navigator. Camera. getpicture (onphotodatasuccess, onfail, {quality: 20, allowedit: True, destinationtype: Camera. destinationtype. file_uri });}//?? From photo library ?? /?? From photo album ????????? Parameters ????????? Function getphoto (source ){//??? ???????????????? Uri navigator. Camera. getpicture (onphotourisuccess, onfail, {quality: 50, destinationtype: destinationtype. file_uri, sourcetype: Source });}//??? ??????????????? Function onfail (mesage) {alert ('failed' because: '+ message) ;}</SCRIPT>
Background code. Net
Protected void page_load (Object sender, eventargs e) {file. writealltext (server. mappath (". ") +"/lg.txt "," 11 "+ datetime. now. tostring () + "\ n \ r"); httppostedfile file = request. files ["fileaddpic"]; // fileaddpic is the input parameter of fileuploadoptions on the app end. This is very important string filename = file. filename; string folder = "~ /Upload "; string uploadpath = httpcontext. Current. server. mappath (Folder +" \ "); If (file! = NULL) {file. saveas (uploadpath + filename); response. Write ("Err") ;}else {response. Write ("OK ");}}
Attachment
Phonegap photo upload photo