【问题标题】:How to use phonegap FileTransfer parameters with .asmx web service如何将 phonegap FileTransfer 参数与 .asmx Web 服务一起使用
【发布时间】:2012-11-07 15:05:13
【问题描述】:

我正在将图像上传到 asmx 网络服务。文件上传工作正常,但我想知道如何访问我在 javascript 中为文件传输设置的参数。

我想将图像编号传递给 asmx SaveImage 网络方法。然后在文件成功保存后,我想将图像编号返回给 Javascript。

//Javascript调用Web服务 函数uploadPhoto(imageURI, imageNumber) {

  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 网络服务

[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";
}

【问题讨论】:

    标签: cordova parameters asmx file-transfer


    【解决方案1】:

    要获取传递给 asmx web 方法的参数,您可以使用 Request.Params...

    我在代码中添加了以下几行

    javascript //添加一个参数,key为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();
    }
    
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2017-02-05
    • 2013-02-08
    相关资源
    最近更新 更多