webuploader秒传的问题

yaotomo 2018-10-24 08:17:14
最近项目上的需求,要实现批量上传图片(经常会有一张10多M的),要用到md5秒传功能
我的思路是,每次上传图片的时候,将图片的md5值保存到数据库表中
控件有个preUpload事件,在每个文件上传前调用,将客户端生成的md5值发送到服务端,如果和数据库中的md5值相同,就跳过不上传这个文件,直接设置为已上传
代码如下


WebUploader.Uploader.register({
"before-send-file": "preUpload"
}, {
preUpload: function (file) {
var owner = this.owner;
var deferred = WebUploader.Deferred();
var blob = file.source.getSource();
owner.md5File(file).fail(function () {
deferred.reject();
}).progress(function (percentage) { // 及时显示进度
//var percent = parseInt(percentage * 100) + '%';
// console.log('Percentage:', percent);
})
.then(function (fileMd5) { // 完成
file.wholeMd5 = fileMd5;
$.ajax({
cache: false,
type: "post",
dataType: "text",
url: "/upload/handler/uploadhandler.ashx?action=checkmd5",
data: {
fileMD5Value: fileMd5,
folderid: folderid,
filename: file.name
},
success: function (result) {
if (result == "1") {//文件存在
deferred.reject();
owner.skipFile(file);
file.uniqueFileName = fileMd5;
//uploader.reset();
//执行失败,执行fail回调函数
console.log("秒传");
} else {
//console.log($.parseJSON(result));
console.log(result);
deferred.resolve(true); //执行成功,执行done回调函数
file.uniqueFileName = fileMd5;
}

}
});
});

//return deferred.promise();
}


当再次上传相同文件时,服务端返回1,会跳过此上传(owner.skipFile(file)),并打印“秒传”
虽然控制台每次都会打印“秒传”,但是偶尔会出现即使是秒传,也会重复再上传一次该文件
也就是说owner.skipFile(file)这句话没有生效。
我这里环境没法调试,只能靠猜测,服务端的代码应该没有问题,也贴出来吧

//上传前检查md5值,如果曾经上传过就秒传
case "checkmd5": {
r = GetReader();
string clientmd5 = context.Request["fileMD5Value"];
string folderid = context.Request["folderid"];
string sqlCheckMd5 = "select * from photomd5 where folderid = '" + folderid + "' and md5val='" + clientmd5 + "'";
DataTable dtCheckMd5 = r.GetTable(sqlCheckMd5);
string response = "no md5 value matched";
try
{
if (dtCheckMd5.Rows.Count > 0)
{
//秒传
response = "1";
//更新鉴定
string filename = context.Request["filename"]; //文件名
MyFiles tMyFile = new MyFiles();
tMyFile.FolderID = folderid; //相册ID
tMyFile.Name = filename;
tMyFile.ID = dtCheckMd5.Rows[0]["photoid"].ToString();
// tMyFile.FileType = "Photo";
SpDictItem spdi = CFHComm.GetSpDIFromFileName(tMyFile);
if (spdi != null)
{
string nickname = GetUserNickname(context.User.Identity.Name, r);
string sqlLastestIdent = "select * from idents where photoid='" + tMyFile.ID + "' order by UpDATETIME desc limit 1";
DataTable dtLatestIdent = r.GetTable(sqlLastestIdent);
string spid = spdi.Key == null ? "" : spdi.Key;
string familyid = spdi.FamilyGenus == null ? "" : spdi.FamilyGenus.FamilyId;
string familyname = spdi.FamilyGenus == null ? "" : spdi.FamilyGenus.FamilyName_Latin;
string genusname = spdi.FamilyGenus == null ? "" : spdi.FamilyGenus.GenusName_Latin;
string genusid = spdi.FamilyGenus == null ? "" : spdi.FamilyGenus.GenusId;
string cfamilyname = spdi.FamilyGenus == null ? "" : spdi.FamilyGenus.FamilyName_Zh;
string cgenusname = spdi.FamilyGenus == null ? "" : spdi.FamilyGenus.GenusName_Zh;
string lname = spdi.Name_Latin == null ? "" : spdi.Name_Latin;
string cname = spdi.Name_Zh == null ? "" : spdi.Name_Zh;
string levelname = spdi.LevelEName == null ? "" : spdi.LevelEName;
if (dtLatestIdent.Rows.Count > 0)
{
string sqlAddIdient = @"update idents set spid= '" + spid + "',familyname='" + familyname + "',genusname='" + genusname + "',cfamilyname='" + cfamilyname + "',cgenusname='" + cgenusname + "',lname='" + lname + "',cname = '" + cname + "',author='" + context.User.Identity.Name + "',truename='" + nickname + "',identtime = sysdate(),cause='' where id='" + dtLatestIdent.Rows[0]["id"].ToString() + "'";
string sqlPhotoUpdate = @"update photo set username='" + context.User.Identity.Name + "', familyid = '" + familyid + "',genusid='" + genusid + "',taxonlevel='" + levelname + "',spid='" + spid + "',lname='" + lname + "',cname='" + cname + "',aname='" + nickname + "',updatetime=sysdate() where id='" + tMyFile.ID + "'";
//files表和location_photo表没有更新时间戳
int res1 = w.Write(sqlAddIdient);
int res2 = w.Write(sqlPhotoUpdate);
if (res1 + res2 >= 2)
{
response = "1";
}
else
{
response = "update idents failed";
}
}
else
{
//没有鉴定信息,添加
string sqlAddIdient = @"insert into `idents` (id,photoid,familyid,genusid,taxonlevel,spid,FamilyName,GenusName,CFamilyName,CGenusName,LName,NameAuthor,EName,CName,AName,Author,TrueName,IdentTime,Cause,Description,updatetime)
VALUES ('" + Guid.NewGuid().ToString() + "','" + tMyFile.ID + "','" + familyid + "','" + genusid + "','" + levelname + "','" + spid + "','" + familyname + "','" + genusname + "','" + cfamilyname + "','" + cgenusname + "','" + lname + "','" + spdi.SAuthor + "','EName','" + cname + "','','" + context.User.Identity.Name + "','" + nickname + "',SYSDATE(),'','',SYSDATE())";
string sqlPhotoUpdate = @"update photo set username='" + context.User.Identity.Name + "', familyid = '" + familyid + "',genusid='" + genusid + "',taxonlevel='" + levelname + "',spid='" + spid + "',lname='" + lname + "',cname='" + cname + "',aname='" + nickname + "',updatetime=sysdate() where id='" + tMyFile.ID + "'";
int res1 = w.Write(sqlAddIdient);
int res2 = w.Write(sqlPhotoUpdate);
if (res1 + res2 >= 2)
{
response = "1";
}
else
{
response = "insert idents failed";
}
}
}
else
{
response = "spdi is null";
}
}
}
catch (Exception ex)
{
response = ex.Message;
}
context.Response.Write(response);
break;
}
...全文
347 点赞 收藏 2
写回复
2 条回复
顺丰小哥 2020年07月20日
检查md5的请求方法里,没有上传的操作
回复 点赞
yaotomo 2018年10月24日
重复文件上传时,查看控制台,发现每次上传请求都是取消,但是仍然会重复上传。检查md5的请求方法里,没有上传的操作。下图是我上传两张图片时候的请求截图。
回复 点赞
发动态
发帖子
JavaScript
创建于2007-09-28

5.1w+

社区成员

22.3w+

社区内容

Web 开发 JavaScript
社区公告
暂无公告