1

我正在尝试将使用 PhoneGap 捕获的图像上传到 MVC4 控制器,即使 PhoneGap 应用程序指示成功,似乎也无法获取该文件。

当我在我的 android 手机上安装它并捕获图像时,消息会更新为“上传完成”,但在 Visual Studio 中有一个断点,我看不到文件进入并且文件没有保存到磁盘。任何帮助将不胜感激。

请参阅以下 PhoneGap 应用程序。

<!DOCTYPE html>
<html>
  <head>
    <title>Capture Photo</title>

   <script src='phonegap.js' type='text/javascript' charset='utf-8'></script>
    <script type='text/javascript' charset='utf-8'>
      var pictureSource;   // picture source
      var destinationType; // sets the format of returned value 

    // Wait for PhoneGap to connect with the device
    //
    document.addEventListener('deviceready',onDeviceReady,false);
    function onDeviceReady() {
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;      
    }

    function capturePhoto() {
      try
      {
          navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
            destinationType: destinationType.FILE_URI});

      } catch(exc){
            alert(exc.message);
      }
    }

    function onPhotoURISuccess(imageURI) {
      var msg = document.getElementById('msg');
      msg.innerText = 'got the image URI: '+ imageURI +' attempting to upload ...';
      try{
        var options = new FileUploadOptions();
        options.fileKey='file';
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
        options.mimeType='image/jpeg';

        var params = new Object();
        params.value1 = 'test';
        params.value2 = 'param';

        options.params = params;

        var ft = new FileTransfer();
        ft.upload(imageURI, 'http://ipaddressofmycomputer/File/UploadFile', win, fail, options);
        msg.innerText = 'upload complete';
      } catch(err){
        var msg = document.getElementById('msg');
        msg.innerText = 'error from catch: '+ err;
      }
    }

    function onFail(message) {
      var msg = document.getElementById('msg');
        msg.innerText = 'Failed because: ' + message;
    }


    function win(r) {
        var msg = document.getElementById('msg');
        msg.innerText = 'Code = ' + r.responseCode +' Response = ' + r.response +' Sent = ' + r.bytesSent;
    }

    function fail(error) {
        var msg = document.getElementById('msg');
        msg.innerText = 'An error has occurred: Code = ' = error.code;
    }

    </script>
  </head>
  <body>
    <button onclick='capturePhoto();'>Capture Photo</button> <br>
    <div id='msg'>Waiting for capture</div>
  </body>
</html>

以下代码是 MVC4 控制器操作。

[HttpPost]
public JsonResult UploadFile()
{

    HttpFileCollectionBase files = Request.Files;

    bool fileSaved = false;

    foreach (string h in files.AllKeys)
    {
        if (files[h].ContentLength > 0)
        {
            string fileName = files[h].FileName;
            int fileSize = files[h].ContentLength;

            string serverPath = Path.Combine(Server.MapPath("~/Content/ImageUploads"));

            if (!Directory.Exists(serverPath))
            {
                Directory.CreateDirectory(serverPath);
            }
            //Get & Save the File
            Request.Files.Get(h).SaveAs(serverPath + fileName);
            fileSaved = true;
        }
    }
    return Json(new { FileSaved = fileSaved });
}
4

0 回答 0