日期:2014-05-18 浏览次数:20657 次
foreach(string upload in Request.Files)
{
if (Request.Files[upload].ContentLength<=0) continue;
string year = DateTime.Now.Year.ToString();
string month = DateTime.Now.Month.ToString("00");
string day = DateTime.Now.Day.ToString("00");
//string fileFolder = string.Concat("Upload/", year, "/", month, "/", day);
string fileFolder = string.Concat(year, "/", month, "/", day);
string path = HostingEnvironment.MapPath("~/" + "Upload/" + fileFolder);
if (!Directory.Exists(path))
{
//在Upload目录下创建了一个文件夹
Directory.CreateDirectory(path);
}
string filename = Request.Files[upload].FileName;
string fileType = Path.GetExtension(Request.Files[upload].FileName).ToLower();//取文件扩展名带"."
string nickName = Path.GetFileName(Request.Files[upload].FileName);
string tempfiletype = fileType.Substring(1);//取文件扩展名不带"."
Random ran = new Random();
string name = DateTime.Now.ToString("yyyyMMdhhmmss") + ran.Next(9999) + fileType;
Request.Files[upload].SaveAs(Path.Combine(path, name));
AddTempAttachment(nickName, "/" + fileFolder + "/" + name, Convert.ToInt32(Session["userid"]), tempfiletype);
}
public void AddTempAttachment(string tempFilename, string tempPath,int tempUserID,string tempFileType)
{
tempAttachment newatt = new tempAttachment() { tempFilename = tempFilename, tempPath = tempPath, tempUerID = tempUserID, tempFileType =tempFileType };
db.tempAttachments.Add(newatt);
db.SaveChanges();
}
@using (Html.BeginForm("AddArticle", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="editor-label">
上传文件:
</div>
<div class="editor-field">
<input name="file1" id="file1" type="file" /><br />
<input name="file2" id="file2" type="file" /><br />
<input name="file3" id="file3" type="file" />
</div>
<p>
<input type="submit" value="添加" />
</p>
}