上传模型方法-断点续传方法

/// <summary>
/// 上传模型方法-断点续传方法
/// </summary>
/// <returns></returns>
public string AddUpload()
{
JsonResult json = new JsonResult();
json.status = false;
try
{
var modelName = HttpContext.Current.Request["modelName"].ToString();
var filetype = HttpContext.Current.Request["filetype"].ToString();
var importType = HttpContext.Current.Request["importType"].ToString();
int importTypeID = int.Parse(importType);//1 新增或覆盖 2 同名称追加模型 append


if (HttpContext.Current.Request.Files.Count <= 0)
{
json.msg = "未识别到上传的文件";
return JsonConvert.SerializeObject(json);
}
HttpPostedFileBase fileBase = new HttpPostedFileWrapper(HttpContext.Current.Request.Files[0]) as HttpPostedFileBase;
long len = fileBase.ContentLength;
string extensionName = System.IO.Path.GetExtension(fileBase.FileName); //获取文件扩展名
string fileName = System.IO.Path.GetFileName(fileBase.FileName); //获取文件名
/////可增加文件格式判断逻辑
if (extensionName.ToUpper() == filetype.ToUpper())
{
//第一步 创建服务ID
var taskID = CreateUpLoadModelTask(modelName);

//第二步 上传文件

 

byte[] bytes = new byte[1024000];//每次分割大小1m 根据情况自由调节,建议10M以内
bool blUplload = true;
using (BinaryReader reader = new BinaryReader(fileBase.InputStream, System.Text.Encoding.UTF8))
{
while (reader.Read(bytes, 0, bytes.Length) > 0)
{
blUplload = blUplload && UpLoadModel(bytes, fileName, taskID, bytes.Length);
}
}

if (blUplload)
{
// 第三步 后台格式转换
ConvertAndUpdateModel(taskID, importTypeID);
//第四步:
json.status = InsertModelData(modelName, modelName);
}
else
{
json.msg = "上传文件失败!";
}

if (UpLoadModel(bytes, fileName, taskID, len))
{
//第三步 后台格式转换
ConvertAndUpdateModel(taskID, importTypeID);
//第四步:

json.status = InsertModelData(modelName, modelName);
}
else
{
json.msg = "上传文件失败!";
}


}
else
{
json.msg = "请上传文件与选定类型不一致";
}

}
catch (Exception ex)
{
json.msg = "服务错误:" + ex.ToString();

}
return JsonConvert.SerializeObject(json);
}
原文地址:https://www.cnblogs.com/efreer/p/13504091.html