微信多图上传的问题

 1 document.querySelector('#upload_img').onclick = function () {
 2         wx.chooseImage({
 3             success: function (res) {
 4                 var i = 0, length = res.localIds.length;
 5                 if (length == 0) {
 6                     alert('请先选择图片');
 7                     return;
 8                 }
 9                 var server_id_list = [];
10                 function upload() {
11                     //for (var i = 0, length = res.localIds.length; i < length; i++) {
12                     wx.uploadImage({
13                         localId: res.localIds[i],
14                         success: function (res) {
15                             i++;
16                             server_id_list.push(res.serverId);
17                             $.Malert('已上传:' + i + '/' + length);
18                             if (server_id_list.length == length) {
19                                 //$.Malert('down:' + server_id_list.length);
20                                 downloadWxPic(server_id_list);
21                             }
22                             if (i < length) {
23                                 upload();
24                             }
25 
26                         },
27                         fail: function (res) {
28                             alert(JSON.stringify(res));
29                         }
30                     });
31                     //}
32                 }
33                 upload();
34             }
35         });
36     };
View Code

千万不要使用for循环进行图片上传,而是要像现在一样用递归的方法进行上传。

不然会在苹果手机上永远只会上传一张图片。

另外,在上传微信后取回本地的过程中,如果使用

WebClient mywebclient = new WebClient();

mywebclient.DownloadFile(requestUrl, filePath);

有时候取回的文件大小只有1K,这时候就是操作系统本身,画图程序一类的也根本无法打开,也就是说文件是有问题的。

不知道有没有人遇到过。如果有的话,请告诉我解决办法。成分感激。

所以取回文件暂时用了以下代码,不过还不知道会不会出现下载文件只有1K的情况

 1                 Stream str = mywebclient.OpenRead(requestUrl);
 2                 StreamReader reader = new StreamReader(str);
 3                 byte[] mbyte = new byte[10000000]; //9.53M
 4                 int allmybyte = (int)mbyte.Length;
 5                 int startmbyte = 0;
 6 
 7                 while (allmybyte > 0)
 8                 {
 9 
10                     int m = str.Read(mbyte, startmbyte, allmybyte);
11                     if (m == 0)
12                         break;
13 
14                     startmbyte += m;
15                     allmybyte -= m;
16                 }
17 
18                 reader.Dispose();
19                 str.Dispose();
20 
21                 FileStream fstr = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
22                 fstr.Write(mbyte, 0, startmbyte);
23                 fstr.Flush();
24                 fstr.Close();
原文地址:https://www.cnblogs.com/jasonlam/p/6099722.html