企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
##ashx处理文件 ``` /// <summary> /// 图片保存操作 /// </summary> /// <param name="basePath"></param> private void SaveImg(string basePath, HttpContext context, string temppath) { var name = string.Empty; basePath = (basePath.IndexOf("~") > -1) ? context.Server.MapPath(basePath) : basePath; HttpFileCollection files = context.Request.Files; if (!Directory.Exists(basePath))//如果文件夹不存在创建文件夹 Directory.CreateDirectory(basePath); var suffix = files[0].ContentType.Split('/'); //var _suffix = suffix[1].Equals("jpeg", StringComparison.CurrentCultureIgnoreCase) ? "" : suffix[1]; var _temp = System.Web.HttpContext.Current.Request["name"]; var fname = System.Web.HttpContext.Current.Request["fname"];//自定义文件名上传 var _suffix = GetFileExt(_temp); //if (!string.IsNullOrEmpty(_temp)) //{ // name = _temp; //} //else //{ Random rand = new Random(24 * (int)DateTime.Now.Ticks); name = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + rand.Next() + "." + _suffix; //} //假如自定义文件名上传 if (!string.IsNullOrEmpty(fname)) { name = fname; } var full = basePath + name; var imgurl = temppath + name; files[0].SaveAs(full); var _result = ""; _result = "{\"msg\" : \"图片上传成功!\", \"result\" : \"true\", \"filename\" : \"" + name + "\", \"imgurl\" : \"" + imgurl + "\"}"; System.Web.HttpContext.Current.Response.Write(_result); } /// <summary> /// 返回文件扩展名,不含“.” /// </summary> /// <param name="_filepath">文件全名称</param> /// <returns>string</returns> public static string GetFileExt(string _filepath) { if (string.IsNullOrEmpty(_filepath)) { return ""; } if (_filepath.LastIndexOf(".") > 0) { return _filepath.Substring(_filepath.LastIndexOf(".") + 1); //文件扩展名,不含“.” } return ""; } ```