asp.Net HTMLフォームFileのパスを取得する方法

3050 ワード

 
  
#region
public void FN_UpFiles()
{
// File
HttpFileCollection files = HttpContext.Current.Request.Files;
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
//
HttpPostedFile postedFile = files[iFile];
string fileName = "";//
//string fileExtension = "";
fileName = Path.GetFileName(postedFile.FileName);// +
int index = fileName.IndexOf(".");
string FileType = fileName.Substring(index).ToLower();//
//FileTypeImg = "../FileTypeimg/" + hz + ".gif";
Guid fileGuid = Guid.NewGuid();// GUID
string NewFileName = fileGuid.ToString();//
NewFileName = NewFileName + FileType;// +
if (postedFile.ContentLength > 2097151 * 1024)//
{
Page.RegisterStartupScript(" ", "alert(' !');return;");
return;
}
else
{
if (fileName != "")//
{
try
{
//
string strpath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName;
try
{
NRModel.File model = new NRModel.File();
NRBLL.File bf = new NRBLL.File();
Guid guid1 = Guid.NewGuid();
Guid guid2 = new Guid(FolderId);
Guid guid3 = Guid.NewGuid();
Guid guid4 = Guid.NewGuid();
model.Fileid = guid1;
model.Folderid = guid2;
model.Filepath = strpath;
model.FileNam = fileName;
model.FileSize = postedFile.ContentLength;
model.Decription = TextArea1.Value.ToString();
model.CreateOn = DateTime.Now;
model.CreateBy = guid3;
model.ModefyBy = guid4;
if (bf.FN_AddNewRes(model) > 0)
{
NR.Error.Log.LogType(" " + fileName + " !" + " :" + strpath);
// ( )
postedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName);
//Page.RegisterStartupScript(" ", "alert(' !');self.opener.location.reload();window.close();");
AlertMsg(" !");
}
}
catch (Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}


}
catch (Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}
}
else
{
Response.Write(" !");
NR.Error.Log.LogType(" !");
}
}


}
}
catch (System.Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}
}
#endregion