百度多文件异步上传控件webuploader基本用法解析

2020-05-24 21:40:27易采站长站整理

<div id="uploader"></div>
<asp:HiddenField ID="hfFilePath" ClientIDMode="Static" runat="server" />

MVC版后端文件接收,即便你是asp.net 引入mvc做ajax也是可以的:


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
public class WebUploaderController : BaseController
{
public ActionResult Process(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
{
string filePathName = string.Empty;string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "UploadDocument");
if (Request.Files.Count == 0)
{
return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失败" }, id = "id" });
}
try
{
filePathName = //自己在这里处理文件保存并返回需要保存到hidden的数据,文件在file或者Request.Files[0] }
catch (Exception)
{
return Json(new { jsonrpc = 2.0, error = new { code = 103, message = "保存失败" }, id = "id" });
}

return Json(new
{
jsonrpc = "2.0",
id = "id",
filePath = urlPath + "/" + filePathName
});
}

     static string urlPath = "../../Upload/Document";

public ActionResult Delete(string filePathName)
{
if (string.IsNullOrEmpty(filePathName))
{
return Content("no");
}
//为了安全 检查一下路径 不够严谨 自行更具业务做更加细致的判断
if (!filePathName.StartsWith(urlPath) ||
filePathName.Substring(6, filePathName.Length - 7).Contains("../"))
{
return Content("no!");
}
string localFilePathName = this.Server.MapPath(filePathName);

try
{
bool b = UploadifyManager.DeleteAttachment(localFilePathName);
if (!b) throw new Exception("删除文件失败");

return Content("ok");
}
catch
{
return Content("no");
}
}
}

 一开始发首页被退下来了,现在重新编辑使内容更加完整,优化了插件代码

 完整demo:  https://github.com/gxrsprite/AspnetMvcWebuploaderDemo

补充:

扩展自定义参数,利用uploadBeforeSend事件可以扩展参数,插件内可根据需要修改。

cookie的问题,我用微软自带的登录系统,不需要做任何特殊处理完全没有问题。