Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

cordova - How to use phonegap FileTransfer parameters with .asmx web service

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";
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...