如何使用的.asmx Web服务的PhoneGap文件传输参数(How to use phonega

2019-07-03 23:48发布

我上传图片到ASMX Web服务。 文件上传工作正常,但我想知道如何访问我在JavaScript的文件传输设置的参数。

我想图像编号传递到ASMX SaveImage Web方法。 然后,该文件已成功保存后,我想将图像编号返回给Javascript。

//调用Java脚本的Web服务功能uploadPhoto(imageURI,图像NUMBER){

  var options = new FileUploadOptions(),
        params = {},
        ft = new FileTransfer(),
        percentLoaded = 0.0,
        progressBar = $(".image" + imageNumber + " > .meter > span");

    options.fileKey = "file";
    options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
    options.mimeType = "image/jpeg";

    params.value1 = "test";
    params.value2 = "param";

    options.params = params;

    //get progress of fileTransfer for progress bar
    ft.onprogress = function (progressEvent) {
       if (progressEvent.lengthComputable) {
           percentLoaded = Math.round(100 * (progressEvent.loaded / progressEvent.total));
           progressBar.width(percentLoaded + "%");
       } else {
           //loadingStatus.increment();
       }
   };
   ft.upload(imageURI, "http://mysite.com/test/uploadPhotos.asmx/SaveImage", win, fail, options);

}

//.asmx Web服务

[WebMethod]
public string SaveImage()
{
    string rootPathRemote = WebConfigurationManager.AppSettings["UploadedFilesPath"].TrimEnd('/', '\\') + "/";
    string rootPhysicalPathRemote = rootPathRemote + "\\";
    int fileCount = 0;

    fileCount = HttpContext.Current.Request.Files.Count;
    for (int i = 0; i < fileCount; i++)
    {
        HttpPostedFile file = HttpContext.Current.Request.Files[i];
        string fileName = HttpContext.Current.Request.Files[i].FileName;
        if (!fileName.EndsWith(".jpg"))
        {
            fileName += ".jpg";
        }
        string sourceFilePath = Path.Combine(rootPhysicalPathRemote, fileName);
        file.SaveAs(sourceFilePath);
    }
    return "test";
}

Answer 1:

为了得到传递给你可以使用Request.Params的ASMX Web方法的参数...

我添加以下行我的代码

JavaScript的//与imageNum的键添加参数

params.imageNum = imageNumber;

加入的.asmx [Web方法]

string allParams = "";
NameValueCollection parameters = HttpContext.Current.Request.Params;
string[] imageNum = parameters.GetValues("imageNum");
for (int j = 0; j < imageNum.Length; j++)
{
    allParams += imageNum[j].ToString();
}


文章来源: How to use phonegap FileTransfer parameters with .asmx web service