62,268
社区成员
发帖
与我相关
我的任务
分享
//判断是否存在文件
if (FileUpload1.HasFile)
{
//删除已经存在的文件
FileInfo file1 = new FileInfo(Server.MapPath("~/UpLoadFile/") + FileUpload1.FileName);
if (file1.Exists)
{
file1.Delete();
}
//文件名
string OldFileName1 = Path.GetFileName(FileUpload1.PostedFile.FileName);
//判断文件格式
string returnstr1 = OldFileName1.Substring(OldFileName1.LastIndexOf('.') + 1);
if (returnstr1.ToLower() != "mp3" && returnstr1.ToLower() != "wma")
{
Response.Write("<script>alert('只能上传格式为mp3|wma格式')</script>");
return;
}
//保存
FileUpload1.SaveAs(Server.MapPath("~/UpLoadFile/") + OldFileName1);
Response.Write("<script>alert('文件以保存')</script>");
}
else
{
Response.Write("<script>alert('请选择上传文件')</script>");
}
if (inputFile.PostedFile.ContentLength > 0)
{
string ServiceUrl = "";
//设定上传文件的保存路径为doc文件夹,请更正为服务器的IP
string strSaveDir = Server.MapPath("../") + "/doc/";
//对应Oracle中的PhysicalPath
string PhysicalPath = "doc";
//取得用户端上完整的档案名称
string strName = inputFile.PostedFile.FileName;
//取得文件名(抱括路径)里最后一个"."的索引 .doc .txt等后缀前的那个“.”
int intExt = strName.LastIndexOf(".");
//取得文件后缀名(扩展名)
string strExt = strName.Substring(intExt);
//根据日期和文件大小不同为文件命名,确保文件名不重复
DateTime datNow = DateTime.Now;
string strNewName1 = datNow.DayOfYear.ToString() + inputFile.PostedFile.ContentLength.ToString();// +strExt;
//取得文件名(包括路径)里最后一个"\"的索引
int intPath = strName.LastIndexOf("\\");
//取得文件名(不包括路径)
string strNewName = "\\" + strNewName1 + strName.Substring(intPath + 1);
//对应Oracle中的FileName
string FileName = strNewName1 + strName.Substring(intPath + 1);
//Server.MapPath()
inputFile.PostedFile.SaveAs(strSaveDir + strNewName);
ServiceUrl = strSaveDir + strNewName;
//這后邊就是插數據庫了
ChkAttachmentID = myCreateFactory.UploadAttachment(AttachmentID, FileName, PhysicalPath, NoticeID, txtAttachmentDes.Text.Trim(), User.Identity.Name.ToString());
if (ChkAttachmentID == 1)
{
labBulletin.Text = "Upload announcement success";
labAttachment.Text = "Upload Annex success";
//Response.Write("<script>(alert('上傳布告和附件成功'))</script>");
}
else
{
labAttachment.Text = "";
labBulletin.Text = "Upload announcement success";
}
}
string SongText = FileUploadUrl.PostedFile.FileName.ToString();
string getFnameSong = SongText.Substring(SongText.LastIndexOf("\\") + 1);
string typeSong = getFnameSong.Substring(getFnameSong.LastIndexOf(".") + 1);
if (typeSong == "mp3" || typeSong == "wma")
{
string url = "Music/Songs/" + Request.QueryString["SID"].ToString() + "/" + Request.QueryString["AID"].ToString() + "/" + model.SongName + "." + typeSong;
FileUploadUrl.PostedFile.SaveAs(Server.MapPath("~/" + url));
model.Songurl = url;
}
else
{
Jscript.Alert("不是MP3/WMA文件");
}
string path = this.Server.MapPath("~/UpLoadFile/");
if (!Directory.Exists(path)) //判断路径是否存在
{
Directory.CreateDirectory(path); //如不存在则创建
}
HttpPostedFile hpf = this.FileUpload1.PostedFile;
hpf.SaveAs(Server.MapPath("~/UpLoadFile/") + OldFileName1);