phonegap 拍照上传照片
js代码 可以完全从 phonegap 官网扣下来
使用的是2.3版本的phonegap
<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:'); //上传上传上传上传上传 try{ var options = new FileUploadOptions(); //用于设置参数,服务端的Request字串 options.fileKey = "fileAddPic"; alert(imageData); options.fileName = imageData.substr(imageData.lastIndexOf('/') + 1); alert('1:' + options.fileName); //如果是图片格式,就用image/jpeg,其他文件格式上官网查API options.mimeType = "image/jpeg"; //这里的uri根据自己的需求设定,是一个接收上传图片的地址 alert('2:' ); 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); } } // ���ɹ��õ�һ����Ƭ��URI���� function onPhotoURISuccess(imageURI) { // ȡ��ע���Բ鿴ͼƬ�ļ���URI // console.log(imageURI); // ��ȡͼƬ��� var largeImage = document.getElementById('largeImage'); // ȡ�����ص�ͼ��Ԫ�� largeImage.style.display = 'block'; // ��ʾ�������Ƭ // ʹ����ǶCSS����������ͼƬ largeImage.src = imageURI; } // ��Capture Photo����ť����¼��������� function capturePhoto() { // ʹ���豸�ϵ�����ͷ���գ������Base64�����ַ�����ʽ��ͼ�� navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI }); } // ��Capture Editable Photo����ť����¼��������� function capturePhotoEdit() { // ʹ���豸�ϵ�����ͷ���գ������Base64�����ַ�����ʽ�Ŀɱ༭ͼ�� navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true, destinationType: Camera.DestinationType.FILE_URI }); } //��From Photo Library��/��From Photo Album����ť����¼��������� 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>
后台代码 。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为app端FileUploadOptions传入参数,此点非常重要 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"); } }