0

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

我想将图像编号传递给 asmx SaveImage web 方法。然后在文件成功保存后,我想将图像编号返回给 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";
}
4

1 回答 1

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();
}
于 2012-11-19T18:18:49.807 回答