/// <summary> /// 上传数据 /// </summary> /// <param name="path"></param> /// <param name="filename"></param> /// <param name="data"></param> /// <param name="encodingStr"></param> /// <param name="rename">为0时不重命名</param> /// <returns></returns> public static ApiRet UploadFile(string url, string path, string filename, byte[] data, string encodingStr = "", string rename = "") { string fileuploadUrl = ""; if (string.IsNullOrEmpty(url)) { //默认是上传图片的地址,不可以上传图片以外的文件 fileuploadUrl = ConfigHelper.UploadUrl; } else { fileuploadUrl = url; } HttpUploadFile u = new HttpUploadFile(); if (!string.IsNullOrEmpty(encodingStr)) { u.SetEncoding(Encoding.GetEncoding(encodingStr)); } string result = u.UploadFile(fileuploadUrl, path, filename, data, rename); ApiRet obj = new ApiRet(); if (!string.IsNullOrEmpty(result)) { obj = JsonHelper.ToObject <ApiRet>(result); } return(obj); }
/** * 上传文件的主处理方法 * @param HttpContext * @param string * @param string[] *@param int * @return Hashtable */ public Hashtable upFile(HttpContext cxt, string pathbase, string[] filetype, int size) { //pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/"; //uploadpath = cxt.Server.MapPath(pathbase);//获取文件上传路径 try { uploadFile = cxt.Request.Files[0]; originalName = uploadFile.FileName; //目录创建 //createFolder(); //格式验证 if (checkType(filetype)) { //不允许的文件类型 state = "\u4e0d\u5141\u8bb8\u7684\u6587\u4ef6\u7c7b\u578b"; } //大小验证 if (checkSize(size)) { //文件大小超出网站限制 state = "\u6587\u4ef6\u5927\u5c0f\u8d85\u51fa\u7f51\u7ad9\u9650\u5236"; } //保存图片 if (state == "SUCCESS") { filename = reName(); byte[] bytesRead = new byte[(int)uploadFile.InputStream.Length]; uploadFile.InputStream.Position = 0; uploadFile.InputStream.Read(bytesRead, 0, bytesRead.Length); uploadFile.InputStream.Close(); filename = reName(); ApiRet uploadResult = NetHelper.UploadFile(ConfigHelper.UploadFileUrl, ConfigHelper.UploadMoudleName, filename, bytesRead, "utf-8"); if (uploadResult.RetCode == "0") { URL = uploadResult.Message.ToString(); } else { state = uploadResult.RetMsg; URL = ""; } //uploadFile.SaveAs(uploadpath + filename); //URL = pathbase + filename; } } catch (Exception e) { // 未知错误 state = "\u672a\u77e5\u9519\u8bef"; URL = ""; } return(getUploadInfo()); }