84346

Upload image from android phonegap to a server using asmx

Question:

I'm trying to upload captured/library image from phonegap application (android) , when am calling asmx web service i got a connection error , note that mobile and server are on the same network

here is my code :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>File Transfer Example</title> <script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script> <script type="text/javascript" charset="utf-8" src="jquery.js"></script> <script type="text/javascript" charset="utf-8"> function getphoto(){ navigator.camera.getPicture(uploadPhoto,function(message) { alert('get picture failed'); },{ quality: 10,destinationType:navigator.camera.DestinationType.FILE_URI,sourceType:navigator.camera.PictureSourceType.PHOTOLIBRARY }); } function uploadPhoto(imageURI) { document.getElementById("myimg").src=imageURI; var options = new FileUploadOptions(); options.chunkedMode = false; options.fileKey="recFile"; var imagefilename = imageURI; options.fileName=imagefilename; options.mimeType="image/jpeg"; // var params = new Object(); //params.value1 = "test"; //params.value2 = "param"; //options.params = params; var ft = new FileTransfer(); alert(imagefilename); //alert(options); //alert(params); ft.upload(imageURI, "http://10.3.150.16/WebSite1/Service.asmx/SaveImage", win, fail, options); } function win(r) { //console.log("Code = " + r.responseCode); //console.log("Response = " + r.response); alert("Sent = " + r.bytesSent); } function fail(error) { switch (error.code) { case FileTransferError.FILE_NOT_FOUND_ERR: alert("Photo file not found"); break; case FileTransferError.INVALID_URL_ERR: alert("Bad Photo URL"); break; case FileTransferError.CONNECTION_ERR: alert("Connection error"); break; } alert("An error has occurred: Code = " + error.code); } </script> </head> <body> <button onclick="getphoto();">get a Photo</button> <button onclick="getphoto();">Upload a Photo</button> <img src="" id="myimg" style="border:1px solid #0f0;height:200px;width:200px;" /> </body> </html>

and here is my asmx we service code:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class Service : System.Web.Services.WebService { [WebMethod] public string SaveImage() { HttpPostedFile file = HttpContext.Current.Request.Files["recFile"]; if (file == null) return null; string targetFilePath = "c:\\deposit\\" + file.FileName; file.SaveAs(targetFilePath); return file.FileName.ToString(); } }

Answer1:

Use the code path.GetFileName(file.FileName), that is:

string targetFilePath = "c:\\deposit\\" + Path.GetFileName(file.FileName);

Answer2:

Could be a firewall issue? Have yo enabled port 80 access to external devices? Note you must host the service in IIS not Kasini (the default visual studio service)

Ta

Ross

Recommend

  • Cordova app navigates to home screen when fetching image from PhotoLibrary
  • upload image using PhoneGap to .NET WCF Rest Service
  • Hi All, Getting the below error. Does anyone have ever faced a similar issue.?
  • android - delay while loading Bitmaps into ArrayList using UniversalImageLoader
  • React Native - Vertical align image with resizeMode “contain”
  • ionic3 tag src [duplicate]
  • .getDownloadUrl() (Not Working Properly “Android Studio” ) [duplicate]
  • Share Image element transition display incorrect size
  • Ionic Cordova camera plugin error on Android
  • Cloudinary - Upload preset must be in whitelist for unsigned uploads
  • Httpclient doesn't correctly caches responses
  • Spring jms failing to connect to Websphere MQ - Resource Exception
  • Windows 8 - Cordova - navigator.camera.getPicture
  • Store image to Firebase storage from cordova camera plugins on Ionic
  • OutOfMemoryError in my gridAdapter
  • How to get String type in setImageResource method?
  • AngularFire upload and get download URL
  • Get Image from Gallery with Intent
  • Cannot create an object of type 'System.Object' from its string representation
  • Picture saving in emulator but not on device
  • Phonegap ios Camera fails on device not simulator
  • Add user ID to PhoneGap File Upload
  • Cordova windows phone app camera orientation is wrong
  • 2d array with two different datatypes?
  • Embedding flash Wimpy player so that it works in Firefox
  • How to Convert Cordova Image Picker Results to Base64 format?
  • How to select sequential duplicates in SQL Server
  • How can i decode an mp3 and encode it as aac with ezstream
  • Fail SonarQube quality gate when coverage decreases
  • How do i disable a text box within an iframe
  • Plotting Route with Multiple Points in iOS
  • write text on image and show it to a imageview
  • Certain Arabic text gets incorrectly shown while other Arabic text gets showed normally?
  • How to change placeholder text in an autocomplete activity of android google place?
  • Check for zero lines output from command over SSH
  • Problems installing Yesod for Haskell
  • Unable to install Git-core+svn by MacPorts
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • ilmerge with a PFX file
  • Setting background image for body element in xhtml (for different monitors and resolutions)