天天看點

asp.net 使用layui上傳

public void Upload(HttpContext context)

{

HttpFileCollection files = context.Request.Files;

string msg = string.Empty;

string error = string.Empty;

if (files.Count > 0)

{

int id = int.Parse(context.Request.Params[“id”]);

if (id > 0)

{

var order = bll.GetModel(id);

string report_directory = HttpContext.Current.Server.MapPath("~/UploadFile/");

string report_path_root = report_directory + “\” + id.ToString() + “\”;

if (!Directory.Exists(report_directory + id.ToString()))

{

Directory.CreateDirectory(report_directory + “\” + id.ToString());

}

string guid = System.Guid.NewGuid().ToString();

string pah = System.IO.Directory.GetCurrentDirectory();

files[0].SaveAs(report_path_root + Path.GetFileName(guid));//檔案保持的路徑 可以使用 files[0].fileName

context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { code = “200”, msg = result!=“OK”?result: " 成功! 檔案大小為:" + files[0].ContentLength, src = files[0].FileName }, Newtonsoft.Json.Formatting.Indented));

context.Response.End();

}

}

}