我尝试使用Phonegap(3.4.0)文件传输和ashx http处理程序将图像上传到远程服务器。我正在使用eclipse并在galaxy s3设备上运行应用程序。 这是我的代码:
JS:
function updateUserImage(userId,imageURI) {
//imageURI is in the format of file:///...
alert("in update()");
window.resolveLocalFileSystemURL(imageURI, onResolveSuccess, fail);
}
function onResolveSuccess(fileEntry) {
var options = new FileUploadOptions(); // PhoneGap object to allow server upload
options.fileKey = "file";
options.fileName = "1000.jpg";
options.mimeType = "image/jpeg"; // file type
options.chunkedMode = false;
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
options.headers = {
Connection: "close"
};
var ft = new FileTransfer();
alert("resolve success");
alert(fileEntry.toURL());
ft.upload(fileEntry.toURL(), encodeURI("http://www.xxxx.com/ReturnValue.ashx"), win, fail, options, true); // Upload
}
function win(r) {
alert("win");
alert(r.response);
}
ASHX:
<%@ WebHandler Language="C#" Class="ReturnValue" %>
using System;
using System.Web;
public class ReturnValue : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Write(context.Request.Files.Count);
}
public bool IsReusable
{
get
{
return false;
}
}
}
logcat的:
05-14 17:56:01.834: V/webview(2125): singleCursorHandlerTouchEvent -getEditableSupport FASLE
05-14 17:56:02.224: W/PluginManager(2125): THREAD WARNING: exec() call to Camera.takePicture blocked the main thread for 56ms. Plugin should use CordovaInterface.getThreadPool().
05-14 17:56:02.224: D/CordovaActivity(2125): Paused the application!
05-14 17:56:02.224: D/CordovaWebView(2125): Handle the pause
05-14 17:56:02.819: W/IInputConnectionWrapper(2125): showStatusIcon on inactive InputConnection
05-14 17:56:08.274: D/CordovaActivity(2125): Incoming Result
05-14 17:56:08.274: D/CordovaActivity(2125): Request code = 34
05-14 17:56:08.274: D/CordovaActivity(2125): We have a callback to send this result to
05-14 17:56:08.314: D/dalvikvm(2125): GC_FOR_ALLOC freed 841K, 11% free 12366K/13831K, paused 16ms, total 24ms
05-14 17:56:08.379: I/dalvikvm-heap(2125): Grow heap (frag case) to 43.056MB for 31961104-byte allocation
05-14 17:56:08.394: D/dalvikvm(2125): GC_FOR_ALLOC freed 6K, 4% free 43571K/45063K, paused 14ms, total 14ms
05-14 17:56:08.394: W/CursorWrapperInner(2125): Cursor finalized without prior close()
05-14 17:56:08.409: D/dalvikvm(2125): GC_CONCURRENT freed <1K, 4% free 43571K/45063K, paused 2ms+3ms, total 16ms
05-14 17:56:10.139: D/dalvikvm(2125): WAIT_FOR_CONCURRENT_GC blocked 0ms
05-14 17:56:10.169: D/dalvikvm(2125): GC_EXPLICIT freed 31267K, 73% free 12321K/45063K, paused 3ms+4ms, total 30ms
05-14 17:56:10.169: E/System(2125): Uncaught exception thrown by finalizer
05-14 17:56:10.174: E/System(2125): java.io.IOException: close failed: EIO (I/O error)
05-14 17:56:10.174: E/System(2125): at libcore.io.IoUtils.close(IoUtils.java:41)
05-14 17:56:10.174: E/System(2125): at java.io.FileInputStream.close(FileInputStream.java:121)
05-14 17:56:10.174: E/System(2125): at java.io.FileInputStream.finalize(FileInputStream.java:142)
05-14 17:56:10.174: E/System(2125): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:186)
05-14 17:56:10.174: E/System(2125): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:169)
05-14 17:56:10.174: E/System(2125): at java.lang.Thread.run(Thread.java:856)
05-14 17:56:10.174: E/System(2125): Caused by: libcore.io.ErrnoException: close failed: EIO (I/O error)
05-14 17:56:10.174: E/System(2125): at libcore.io.Posix.close(Native Method)
05-14 17:56:10.174: E/System(2125): at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
05-14 17:56:10.174: E/System(2125): at libcore.io.IoUtils.close(IoUtils.java:38)
05-14 17:56:10.174: E/System(2125): ... 5 more
05-14 17:56:10.174: W/CursorWrapperInner(2125): Cursor finalized without prior close()
05-14 17:56:10.174: I/CordovaLog(2125): Changing log level to DEBUG(3)
05-14 17:56:10.179: D/Whitelist(2125): Unlimited access to network resources
05-14 17:56:10.179: D/CordovaActivity(2125): Resuming the App
05-14 17:56:10.179: D/CordovaActivity(2125): CB-3064: The errorUrl is null
05-14 17:56:10.234: D/CordovaLog(2125): file:///android_asset/www/index.html: Line 23 : file:///storage/sdcard0/Android/data/com.phonegap.helloworld/cache/1400079368295.jpg
05-14 17:56:10.234: I/Web Console(2125): file:///storage/sdcard0/Android/data/com.phonegap.helloworld/cache/1400079368295.jpg:23
05-14 17:56:10.289: D/dalvikvm(2125): GC_CONCURRENT freed 42K, 72% free 12737K/45063K, paused 2ms+13ms, total 28ms
05-14 17:56:10.304: D/SoftKeyboardDetect(2125): Ignore this event
05-14 17:56:10.454: D/SoftKeyboardDetect(2125): Ignore this event
05-14 17:56:16.739: D/FileTransfer(2125): upload file:///storage/sdcard0/Android/data/com.phonegap.helloworld/cache/1400079368295.jpg to http://www.xxxx.com/ReturnValue.ashx
05-14 17:56:16.739: D/FileTransfer(2125): fileKey: file
05-14 17:56:16.739: D/FileTransfer(2125): fileName: 1000.jpg
05-14 17:56:16.739: D/FileTransfer(2125): mimeType: image/jpeg
05-14 17:56:16.739: D/FileTransfer(2125): params: {"value1":"test","value2":"param"}
05-14 17:56:16.739: D/FileTransfer(2125): trustEveryone: true
05-14 17:56:16.739: D/FileTransfer(2125): chunkedMode: false
05-14 17:56:16.739: D/FileTransfer(2125): headers: {"Connection":"close"}
05-14 17:56:16.739: D/FileTransfer(2125): objectId: 1
05-14 17:56:16.739: D/FileTransfer(2125): httpMethod: POST
05-14 17:56:16.764: D/FileTransfer(2125): Content Length: 358920
05-14 17:56:34.384: D/dalvikvm(2125): GC_CONCURRENT freed 150K, 72% free 12974K/45063K, paused 8ms+5ms, total 47ms
05-14 17:56:58.089: D/FileTransfer(2125): Uploaded 114920 of 358920 bytes
05-14 17:57:15.299: D/FileTransfer(2125): Uploaded 229608 of 358920 bytes
05-14 17:57:17.069: D/FileTransfer(2125): Uploaded 344296 of 358920 bytes
05-14 17:57:17.704: D/FileTransfer(2125): Sent 358920 of 358920
05-14 17:57:22.229: D/FileTransfer(2125): response code: 200
05-14 17:57:22.234: D/FileTransfer(2125): response headers: {null=[HTTP/1.1 200 OK], Cache-Control=[private], Connection=[close], Content-Type=[text/html; charset=utf-8], Date=[Wed, 14 May 2014 14:57:33 GMT], OkHttp-Received-Millis=[1400079442236], OkHttp-Response-Source=[NETWORK 200], OkHttp-Selected-Transport=[http/1.1], OkHttp-Sent-Millis=[1400079441802], Server=[Microsoft-IIS/8.0], Vary=[Accept-Encoding], X-AspNet-Version=[4.0.30319], X-Powered-By=[ASP.NET], X-Powered-By-Plesk=[PleskWin]}
05-14 17:57:22.239: D/FileTransfer(2125): got response from server
05-14 17:57:22.239: D/FileTransfer(2125): 0
05-14 17:57:22.334: D/dalvikvm(2125): GC_CONCURRENT freed 778K, 72% free 12691K/45063K, paused 5ms+4ms, total 29ms
05-14 17:
我的config.xml中包含<access origin="*"/>
,清单文件包含<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
。
我收到回复&#34; 0&#34;从服务器,这意味着它没有得到任何文件。但另一方面,logcat说&#34; 358920发送358920&#34;。
任何帮助解决这个问题都将非常感激。