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