我正在将图片上传到asmx网络服务。文件上传工作正常,但我想知道如何访问我在javascript中为filetransfer设置的参数。
我想将图像编号传递给asmx SaveImage Web方法。然后在文件成功保存后,我想将图像编号返回给Javascript。
// Javascript调用Web服务 function 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 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";
}
答案 0 :(得分:1)
要获取传递给asmx web方法的参数,可以使用Request.Params ...
我在代码中添加了以下行
javascript //添加一个带有imageNum键的参数
params.imageNum = imageNumber;
已添加到.asmx [网络方法]
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();
}