在服务器端asp.net mvc的iphone phonegap应用程序中出错Filetranser

时间:2022-10-15 08:22:52

Following this tutorial, I created iPhone client side for file transfer. I could be able to choose the file to be uploaded but on uploading to server there occur an error as follows.

在本教程之后,我创建了用于文件传输的iPhone客户端。我可以选择要上传的文件,但在上传到服务器时会出现如下错误。

2013-03-01 19:38:35.841 StampedeTest[1687:c07] FileTransferError {
code = 3;
"http_status" = 405;
source = "file://localhost/Users/davidroper/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/5C506A40-959F-4A15-8D01-B3343EDB3257/tmp/cdv_photo_004.jpg";
target = "http://******.azurewebsites.net/api/FileUploadTest";
}

client side code for uploading

用于上传的客户端代码

 function uploadPhoto(imageURI) {
        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;
        options.chunkedMode = false;

        var ft = new FileTransfer();
        ft.upload(imageURI, "http://*****.azurewebsites.net/api/FileUploadTest", win, fail, options);
    }

Serverside code

public class FileUploadTestController : ApiController
{ 
    [WebMethod]
    public string Getupload()
    {
        if (HttpContext.Current.Request.Files["file"] != null)
        {
            HttpPostedFile file = HttpContext.Current.Request.Files["file"];
            string targetFilePath = "http://stampedemvc.azurewebsites.net/Content/img/" + file.FileName;
            file.SaveAs(targetFilePath);

            return file.FileName.ToString();

        }
        else {

            return "Error";

        }
    }
}

How can i make this working?

我怎样才能使这个工作?

Thanks.

1 个解决方案

#1


0  

I got it working!

我搞定了!

I change the apicontroller to controller

我将apicontroller更改为控制器

working code

    [HttpPost]
    public string Getupload()
    {

        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\\img\\"));

                if (!Directory.Exists(serverPath))
                {
                    Directory.CreateDirectory(serverPath);
                }

                try
                {
                    //Get & Save the File
                    Request.Files.Get(h).SaveAs(serverPath + fileName);
                    fileSaved = true;

                }
                catch (Exception ex)
                {

                }

            }
        }

        return "Success";
    }

#1


0  

I got it working!

我搞定了!

I change the apicontroller to controller

我将apicontroller更改为控制器

working code

    [HttpPost]
    public string Getupload()
    {

        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\\img\\"));

                if (!Directory.Exists(serverPath))
                {
                    Directory.CreateDirectory(serverPath);
                }

                try
                {
                    //Get & Save the File
                    Request.Files.Get(h).SaveAs(serverPath + fileName);
                    fileSaved = true;

                }
                catch (Exception ex)
                {

                }

            }
        }

        return "Success";
    }