public void DefaultCatch(HttpContext Handler, UploadDetail Detail) { //Handler.Response.StatusCode = typeCode; Handler.Response.ContentType = "Application/JSON"; Handler.Response.Write(Detail.toJSON("error", parent.Message)); Handler.Response.End(); }
public const int bufferTime = 20; // 20ms // 处理请求主函数 public void ProcessRequest(HttpContext context) { string method = ""; UploadDetail detail = null; try { detail = (UploadDetail)context.Session["UploadDetail"]; } catch (Exception e) { detail = new UploadDetail(); context.Session["UploadDetail"] = detail; detail = (UploadDetail)context.Session["UploadDetail"]; } try // 总异常捕捉 { // 过滤空请求 try { method = context.Request.QueryString["method"]; } catch (Exception) { throw new UploadException("请求为空!" /*, 400*/); } switch (method) { case "doUpload": HttpRequest request = context.Request; HttpPostedFile fileHandler = null; try { fileHandler = request.Files[0]; } catch (Exception) { throw new UploadException("获取上传文件流失败!" /*, 411*/); } if (fileHandler.ContentLength <= 0) { throw new UploadException("没有选择文件!" /*, 411*/); } else if (!formatCheck(fileHandler)) { throw new UploadException("文件格式不妥。请上传正常的文档!" /*, 406*/); } else if (request.ContentLength > maxUploadSize) { throw new UploadException("文件过大,上传失败!" /*, 413*/); } else if (detail.isReady) { throw new UploadException("正在进行另一上传进程,请稍后再试。" /*, 409*/); } else { detail.isReady = true; detail.total = fileHandler.ContentLength; string savePath = (new Random().Next(100000).ToString()) + System.IO.Path.GetExtension(fileHandler.FileName); savePath = context.Server.MapPath("~/uploadTemp/" + savePath); fileWriter(savePath, fileHandler.InputStream, detail); } break; case "getStat": if (!detail.isReady) { throw new UploadException("您还没有上传,不能获取进度!" /*, 403*/); } else { context.Response.ContentType = "Application/JSON"; context.Response.Write(detail.toJSON()); context.Response.End(); } break; default: break; } } catch (UploadException e) { e.DefaultCatch(context, detail); } }
// 处理请求主函数 public void ProcessRequest(HttpContext context) { string method = ""; UploadDetail detail = null; try { detail = (UploadDetail)context.Session["UploadDetail"]; } catch (Exception e) { detail = new UploadDetail(); context.Session["UploadDetail"] = detail; detail = (UploadDetail)context.Session["UploadDetail"]; } try // 总异常捕捉 { // 过滤空请求 try { method = context.Request.QueryString["method"]; } catch (Exception) { throw new UploadException("请求为空!"/*, 400*/); } switch (method) { case "doUpload": HttpRequest request = context.Request; HttpPostedFile fileHandler = null; try { fileHandler = request.Files[0]; } catch(Exception) { throw new UploadException("获取上传文件流失败!"/*, 411*/); } if (fileHandler.ContentLength <= 0) { throw new UploadException("没有选择文件!"/*, 411*/); } else if (!formatCheck(fileHandler)) { throw new UploadException("文件格式不妥。请上传正常的文档!"/*, 406*/); } else if (request.ContentLength > maxUploadSize) { throw new UploadException("文件过大,上传失败!"/*, 413*/); } else if (detail.isReady) { throw new UploadException("正在进行另一上传进程,请稍后再试。"/*, 409*/); } else { detail.isReady = true; detail.total = fileHandler.ContentLength; string savePath = (new Random().Next(100000).ToString()) + System.IO.Path.GetExtension(fileHandler.FileName); savePath = context.Server.MapPath("~/uploadTemp/" + savePath); fileWriter(savePath, fileHandler.InputStream, detail); } break; case "getStat": if (!detail.isReady) throw new UploadException("您还没有上传,不能获取进度!"/*, 403*/); else { context.Response.ContentType = "Application/JSON"; context.Response.Write(detail.toJSON()); context.Response.End(); } break; default: break; } } catch (UploadException e) { e.DefaultCatch(context, detail); } }