1

我正在使用Intel XDK New开发应用程序。我遇到的问题涉及 Intel XDK JavaScript Bridge API,尤其是uploadToServer 方法

我使用 C# MVC4 编写了一个简单的服务器后端。我有上传从移动设备拍摄的照片的工作代码,但是当我在服务器上设置上传 url 需要身份验证时,代码失败。

我不确定 MVC 身份验证是如何工作的,我认为它混合使用了 cookie 和一些 http 标头?为了澄清我正在使用MVC Forms Authentication。但是,服务器并没有真正使用表单或返回视图。我已将身份验证所需事件修改为仅返回 404 而不是视图。

真正的问题是,熟悉英特尔 XDK 的人是否知道 uploadToServer 方法是否也可以通过身份验证 cookie/标头/不管它如何工作?到目前为止,我对服务器的所有请求都使用了intel appframework $.ajax 方法。(不是 jQuery,它只是看起来像它)。这似乎通过了身份验证,因为我在服务器上有返回需要身份验证的 Json 的方法,并且一切正常。

我的服务器代码:

[HandleError]
[MyAuthorizeAttribute]
public class PhotosController : Controller
{
    public ActionResult UploadPhoto()
    {
        Log.Write("UploadPhoto");
        if (Request != null && Request.Files != null && Request.Files.Count > 0)
        {
            foreach (string fileName in Request.Files)
            {
                if (Request.Files[fileName] == null || Request.Files[fileName].ContentType != "image/jpeg") 
                    continue;

                Request.Files[fileName].SaveAs(Server.MapPath("~/Content/Images/photo.jpg"));
            }
            return Json("File(s) Uploaded", JsonRequestBehavior.AllowGet);
        }
        return Json("No File(s) to Upload", JsonRequestBehavior.AllowGet);
    }
}

我的 JavaScript

document.addEventListener("intel.xdk.file.upload.busy", function(evt) { $('#afui').popup("Sorry, a file is already being uploaded"); });
document.addEventListener("intel.xdk.file.upload",UploadComplete);
document.addEventListener("intel.xdk.file.upload.cancel", function(evt) { $('#afui').popup("File upload was cancelled "+evt.localURL); });

function MyUploadMethod() {    
    intel.xdk.file.uploadToServer(pictureURL,"http://mywebsite.co.uk/Photos/UploadPhoto", "", "image/jpeg", "UpdateUploadProgress");
}

function UpdateUploadProgress(bytesSent,totalBytes)
{
   if(totalBytes>0) {
        currentProgress=(bytesSent/totalBytes)*100;
        document.getElementById("progress").innerHTML=currentProgress+"%";
        console.log(currentProgress);
   }           
}

function UploadComplete(evt)
{
    if(evt.success===true)
    {
        $('#afui').popup("Success");
    }
    else {
        $('#afui').popup("Error uploading file "+evt.message);
    }
    document.getElementById("progress").innerHTML="";
}
4

1 回答 1

0

好吧,我认为您应该尝试一下,我曾将它用于我以前使用 CORS 技术的项目。

Response.AppendHeader("Access-Control-Allow-Origin", "*");

于 2015-04-07T20:43:42.853 回答