- [Post]
- public async Task UpBase64Image()
- {
- IFormCollection postdata = await this.Context.Request.ReadFormAsync(new CancellationToken());
- StringValues stringValues = postdata["base64Str"];
- string base64Str = ((StringValues) ref stringValues)[0];
- base64Str = base64Str.Substring(base64Str.IndexOf(",") + 1);
- stringValues = postdata["FileName"];
- string FileName = ((StringValues) ref stringValues)[0];
- byte[] arr = Convert.FromBase64String(base64Str);
- MyApi1.MyApi1.uploadResult result = new MyApi1.MyApi1.uploadResult();
- string assemblyLocation = Assembly.GetExecutingAssembly().Location;
- if (AppDomain.CurrentDomain.ShadowCopyFiles)
- {
- Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
- assemblyLocation = uri.LocalPath;
- uri = (Uri) null;
- }
- string dir = Path.GetDirectoryName(assemblyLocation);
- DirectoryInfo theFolder = new DirectoryInfo(dir);
- DirectoryInfo path = theFolder.Parent;
- string saveFilePath = path?.ToString() + "\\GeneratedResources\\Upload";
- if (!Directory.Exists(saveFilePath))
- Directory.CreateDirectory(saveFilePath);
- string upFileName = FileName;
- char[] a = new char[1]{ '\\' };
- string file_name = DateTime.Now.ToFileTime().ToString() + "_" + upFileName + ".jpg";
- if (file_name == "")
- {
- saveFilePath = "";
- result.code = -1;
- result.msg = "客户端文件不存在!";
- postdata = (IFormCollection) null;
- base64Str = (string) null;
- FileName = (string) null;
- arr = (byte[]) null;
- result = (MyApi1.MyApi1.uploadResult) null;
- assemblyLocation = (string) null;
- dir = (string) null;
- theFolder = (DirectoryInfo) null;
- path = (DirectoryInfo) null;
- saveFilePath = (string) null;
- upFileName = (string) null;
- a = (char[]) null;
- file_name = (string) null;
- }
- else
- {
- saveFilePath = Path.Combine(saveFilePath, file_name);
- using (MemoryStream ms = new MemoryStream(arr))
- {
- Bitmap bmp2 = new Bitmap((Stream) ms);
- bmp2.Save(saveFilePath, ImageFormat.Jpeg);
- bmp2.Dispose();
- bmp2 = (Bitmap) null;
- }
- result.code = 0;
- result.msg = "上传成功!";
- result.src = saveFilePath;
- Dictionary<string, object> values = new Dictionary<string, object>();
- string src = "/GeneratedResources/UpLoad/" + file_name;
- values.Add("图片", (object) src);
- this.DataAccess.AddTableData("表2", values);
- values = (Dictionary<string, object>) null;
- src = (string) null;
- postdata = (IFormCollection) null;
- base64Str = (string) null;
- FileName = (string) null;
- arr = (byte[]) null;
- result = (MyApi1.MyApi1.uploadResult) null;
- assemblyLocation = (string) null;
- dir = (string) null;
- theFolder = (DirectoryInfo) null;
- path = (DirectoryInfo) null;
- saveFilePath = (string) null;
- upFileName = (string) null;
- a = (char[]) null;
- file_name = (string) null;
- }
- }
- public class uploadResult
- {
- public int code { get; set; }
- public string msg { get; set; }
- public string src { get; set; }
- }
复制代码 这是工程里反编译出来的,跟原来的应该不一样,大致逻辑可以参考的 |