【问题标题】:Cannot upload files from android to asp.net server using Cordova FileTransfer plugin无法使用 Cordova FileTransfer 插件将文件从 android 上传到 asp.net 服务器
【发布时间】:2016-06-10 12:18:21
【问题描述】:

我有一个使用 cordova 文件传输插件创建的简单移动应用程序。下面是上传代码

function uploadPhoto(fileURI) {
            var options = new FileUploadOptions();
            options.fileKey = fileURI.substr(fileURI.lastIndexOf('/') + 1);
            options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);

            if (cordova.platformId == "android") {
                options.fileName += ".jpg" 
            }

            options.mimeType = "image/jpeg";
            //options.contentType = 'multipart/form-data';
            options.params = {}; // if we need to send parameters to the server request 

            options.headers = {
                Connection: "Close"
            };
            //options.httpMethod = 'POST';
            //options.chunkedMode = false;

            var ft = new FileTransfer();

            rst.innerHTML = "Upload in progress...";
            ft.upload(
                fileURI,
                encodeURI("http://localhost:55013/virtualroomservice.asmx/SaveImage"),
                onFileUploadSuccess,
                onFileTransferFail,
                options, true);

            function onFileUploadSuccess (result) {
               // rst.innerHTML = "Upload successful";
                console.log("FileTransfer.upload");
                console.log("Code = " + result.responseCode);
                console.log("Response = " + result.response);
                console.log("Sent = " + result.bytesSent);
                console.log("Link to uploaded file: https://www.kinrep.com/foster/ws/contentlibrary" + result.response);
                var response = result.response;
                var destination = "https://www.kinrep.com/foster/WS/ContentLibrary" + response.substr(response.lastIndexOf('=') + 1);
                if(this.id == 'uploadcheque') {
                    document.getElementById("hdnchequeimgpath").value = destination;

                } else if(this.id == 'uploaddoorlock') {

                    document.getElementById("hdndoorlockedimgpath").value = destination;
                } else {

                    document.getElementById("hdnothersimgpath").value = destination;
                }
                rst.innerHTML = "File uploaded to: " +
                                                              destination + 
                                                              "</br><button class=\"button\" onclick=\"window.open('" + destination + "', '_blank', 'location=yes')\">Open Location</button>";
                //document.getElementById("downloadedImage").style.display="none";
            }

            function onFileTransferFail (error) {
                rst.innerHTML = "File Transfer failed: " + error.code;
                console.log("FileTransfer Error:");
                console.log("Code: " + error.code);
                console.log("Source: " + error.source);
                console.log("Target: " + error.target);
            }
}

下面是服务器代码

[WebMethod]
[ScriptMethod]
public string SaveImage()
{
    try
    {
        HttpPostedFile file = HttpContext.Current.Request.Files[0];
        if (file == null)
            return "0";

        string targetFilePath = Server.MapPath(@"WS\ContentLibrary") + file.FileName;
        file.SaveAs(targetFilePath);
    }
    catch (Exception ex)
    {
        string s = ex.Message;
        return s;
    }

    return "1";

}

当调用被调用时,它进入 SaveImage webmethod,但 HttpContext.Current.Request.Files.Count 为 0。当我指向 filedropper.com 时,如示例代码中给出的相同调用它工作正常(我可以看到在 filedrop.com 上上传的图像)但在指向我的 Windows Web 服务时不起作用。我看过其他各种帖子,但无法弄清楚出了什么问题。在客户端控制台中,它不会写入发送的字节数,这意味着客户端没有问题,而服务器端似乎有问题。谁能提出问题出在哪里?

下面是调试输出

UPDATE-06112016-5:35PMIS 还是一头雾水也发在http://www.telerik.com/forums/file-upload-not-working-93d711a97c9b

UPDATE-06112016-9-54PMIS

在噩梦中无法弄清楚如何解决这个问题后,我决定在 iis 上托管一个 php 作为替代方案。 Cordova 文件传输插件似乎与herehere 的 php 服务器页面一起正常工作

【问题讨论】:

  • 不太确定这是否是防火墙问题,但刚刚确认 Windows 防火墙在 localhost 运行的地方已关闭
  • 尝试使用ip代替主机名(localhost)
  • @Gandhi 无效的问题与 127.0.0.1 相同。我认为问题在于请求标头无法在两个世界之间很好地遵守
  • 好的。尝试在 options.headers = { 'Content-Type': 'image/jpg' } 中添加这个选项并检查一次
  • 我在您的代码中注意到的另一件事是 options.headers = {Connection: "Close" };理想情况下应该是 options.headers = {'Connection':'Close' };

标签: asp.net cordova file-upload cordova-plugins jquery-file-upload


【解决方案1】:

显然,快速 CGI 模式下的 IIS 7.5 有一个关于分块多部分表单数据的错误:https://bugs.php.net/bug.php?id=60826

Cordova 的 FileTransfer 插件默认分块模式为 true:

chunkedMode:是否以分块流模式上传数据。默认为真。 (布尔)

在我的情况下,这正是问题所在,将 options.chunkedMode 设置为 true 立即修复了它。

请注意,您将无法再使用 onprogress 属性。

onprogress:在传输新数据块时使用 ProgressEvent 调用。 (功能)

【讨论】:

  • 尚未尝试使用此设置,有一段时间我将其放在一边。谢谢通过'
猜你喜欢
  • 2017-04-18
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
相关资源
最近更新 更多