phonegap上传图片并传参

本文介绍如何使用PhoneGap上传图片,并附带额外参数至.NET服务端。文章详细展示了客户端设置参数与上传流程,以及服务端接收参数和处理图片的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

phonegap上传图片并传参

[1].[代码] [其他]代码 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
phonegap上传图片并传参(服务端是.NET 做的一般处理程序ashx)
phonegap 上传图片传参
                 var params = new Object();
                 params.value1 = "test";
                 params.value2 = "param";
 
                 options.params = params;
phonegap客户端(这里只写了上传的代码,调用机器API的就不写了)
                 var imageURI = document.getElementById('myImage').src;
                 var options = new FileUploadOptions();
                 options.fileKey = "file";
                 var imagefilename = Number(new Date()) + ".jpg";
                 options.fileName = imagefilename;
                 //options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1); 
                 options.mimeType = "image/jpeg";
 
                 var params = new Object();
                 params.value1 = "test";//此处设置要上传的参数
                 params.value2 = "param";
 
                 options.params = params;
                 var url = "http://192.168.2.99/demo/upload.ashx?jsoncallback=?"
                 var ft = new FileTransfer();
                 ft.upload(imageURI, url, win, fail, options, true);
ashx 服务端
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class upload : IHttpHandler {
     
     public void ProcessRequest (HttpContext context) {
         context.Response.ContentType = "text/plain";
         string callbackMethodName = context.Request.Params["jsoncallback"];
 
         var Value1 = context.Request["value1"];//这里就是获取得到phonegap上传的传的参数
         var Value2 = context.Request["value2"];//这里就是获取得到phonegap上传的传的参数
         if (context.Request.Files.Count > 0)
         {
             //取到文件对象
             HttpPostedFile file = context.Request.Files[0];
             //取得文件后缀名(带个点)
             string ext = System.IO.Path.GetExtension(file.FileName);
 
             //判断上传文件的类型(jpeg),这里是根据文件头判断的,防止用户上传恶意假图
             if (file.ContentType == "image/jpeg" || file.ContentType == "image/pjpeg" || file.ContentType == "image/png" || file.ContentType == "image/gif")
             {
                 //给文件取随及名
                 Random ran = new Random();
                 string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ran.Next(100, 1000) + ext;
 
                 //保存文件
                 string path = context.Request.MapPath("images/" + fileName);
                 file.SaveAs(path);
 
                 //提示上传成功
                 string result = callbackMethodName + "({\"id\":" + "\"1\"})";
                 context.Response.Write(result);
             }
             else
             {
                 string result = callbackMethodName + "({\"id\":" + "\"1\"})";
                 context.Response.Write(result);
             }
         }
         else
         {
             string result = callbackMethodName + "({\"id\":" + "\"1\"})";
             context.Response.Write(result);
         }
 
     }
  
     public bool IsReusable {
         get {
             return false;
         }
     }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值