0

这是我的 javascript 代码:

var fileURL = "file://" + mFileListURL[0].fullPath;
var options = new FileUploadOptions();
options.fileKey = "recFile";
var imagefilename = Number(new Date()) + ".jpg";
options.fileName = imagefilename;
options.mimeType = "image/jpeg";    
var params = new Object();      
options.params = params;

var ft = new FileTransfer();
ft.upload(fileURL,"http://mywebserver/UploadFoto.asmx/SaveImage",
                function(r) {
                    alert("It's OK!");
                    alert("Response = " + r.response);

                }, function(error) {
                    alert("An error has occurred: Code = "
                            + error.code);
                }, options);

这个服务器端代码:

        [WebMethod]
        [GenerateScriptType(typeof(String))]
        [ScriptMethod]
        public String SaveImage()
        {
            HttpContext context = HttpContext.Current;
            if (context.Request.Files.Count > 0)
            {
                HttpFileCollection files = context.Request.Files;
                foreach (string key in files)
                {
                    HttpPostedFile file = files[key];
                    string fileName = file.FileName;
                    if (fileName != null && fileName != "")
                    {                        
                        String fileStored = System.IO.Path.Combine(context.Server.MapPath("~/public/"), fileName);                           
                        file.SaveAs(fileStored);                       
                    }
                }
            }            
            return "Filestored OK";
        }

现在,图像上传完成,但我没有收到返回的字符串,没有来自服务器的响应,也没有错误代码。我也使用了 Json 响应,但什么也没有(图片上传,没有响应,没有返回字符串)。怎么了?

谢谢。国际文凭

4

2 回答 2

0

WebMethod 必须是静态的

[WebMethod]
        [GenerateScriptType(typeof(String))]
        [ScriptMethod]
        public static String SaveImage()
        {
            HttpContext context = HttpContext.Current;
            if (context.Request.Files.Count > 0)
            {
                HttpFileCollection files = context.Request.Files;
                foreach (string key in files)
                {
                    HttpPostedFile file = files[key];
                    string fileName = file.FileName;
                    if (fileName != null && fileName != "")
                    {                        
                        String fileStored = System.IO.Path.Combine(context.Server.MapPath("~/public/"), fileName);                           
                        file.SaveAs(fileStored);                       
                    }
                }
            }            
            return "Filestored OK";
        }
于 2015-04-20T12:49:49.830 回答
0

尝试这个

function(r) 
{
  alert("It's OK!");
  alert("Sent = " + r.bytesSent);
}
于 2014-04-23T07:13:11.443 回答