问题
I am uploading an image to an asmx web service. The file upload works fine, but I am wondering how to access the parameters that I set in the javascript for the filetransfer.
I want to pass the image number to the asmx SaveImage web method. Then after the file has successfully been saved I want to return the image number to the Javascript.
//Javascript Calling Web Service 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 service
[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";
}
回答1:
To get the parameters passed to the asmx web method you can use the Request.Params...
I added the following lines to my code
javascript //add a parameter with a key of imageNum
params.imageNum = imageNumber;
added to .asmx [Web Method]
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();
}
来源:https://stackoverflow.com/questions/13458099/how-to-use-phonegap-filetransfer-parameters-with-asmx-web-service