上传图片

namespace JngsWeb.Web.Interface
{
public partial class fileupload : System.Web.UI.Page
{
MultipartDataInfo mdi;
protected void Page_Load(object sender, EventArgs e)
{

SendResponse(Response, Dopost());
}

private void SendResponse(HttpResponse response, string data)
{
response.Clear();
response.Cache.SetCacheability(HttpCacheability.NoCache);
response.Buffer = true;
response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
response.Cache.SetExpires(DateTime.Now.AddDays(-1));
response.Expires = 0;
response.CacheControl = "no-cache";
response.Cache.SetNoStore();
response.Write(data);
response.Flush();
response.End();
}

private string Dopost()
{
JsonData jd = new JsonData();
JsonBase.AddJsonArray("result", ref jd);
try
{
mdi = new MultipartDataInfo();

mdi.CollectFormDatas();
string img = "";
string FileRealName = "";
string FileServerName = "";
string path;
path = "../filehtml/";
mdi.RootPath = path;

if (mdi.FileCount > 0)
{

mdi.Save();
foreach (MyFileInfo mfi in mdi.FileCollections)
{
try
{
FileServerName = mfi.FileName;
FileRealName = mfi.SFileName;

Dictionary<string, string> dict = new Dictionary<string, string>();
string parent_id = mdi.FormCollections["parent_id"];

string img_type = mdi.FormCollections["img_type"];
string img_title = mdi.FormCollections["img_title"];
string normal_img = "filehtml/" + FileServerName;

if (string.IsNullOrEmpty(img))
{
img = normal_img;
}
else {
img = "" + img + "," + normal_img + "";
}
string timg = Server.MapPath(path + FileServerName);

if (img_type == "tousu_123")
{
string sql = "select * from tousu_img where tousuid=" + parent_id + "";
var dt = BaseDal.QueryDataTable(sql);
if (dt.Rows.Count == 0)
{
dict.Add("attach_url", img);
dict.Add("attach_name", img_title);
dict.Add("tousuid", parent_id);
JngsDal.InsertToTable("tousu_img", dict);
jd["result"].Add("filehtml/" + FileServerName);

}
else {

dict.Add("attach_url", img);

JngsDal.UpdateTables("tousu_img", dict, "tousuid=" + parent_id);
jd["result"].Add("filehtml/" + FileServerName);
}

}

}
catch (Exception ex)
{
JngsDal.RecordError("fileupload", ex.Message);
}
}
}
}

catch (Exception ex)
{
JngsDal.RecordError("fileupload1", ex.Message);
}

return jd.ToJson();
}
}
}

posted @ 2018-10-20 09:31  程非成  阅读(177)  评论(0编辑  收藏  举报