求 fileupload 上传文件的代码 ,不只上传图片,还可以上传其它文件
解决方案 »
- 麻烦帮我看下这个问题
- |zyciis| 200分求救;新网站,才上线.大家来打渣,出现的严重问题 如何为复杂的查询分页进行传参, 防止拼接语句
- ??????????????webchart控件问题,大家帮帮我啊,我向来结贴给分的,大家帮帮我啊??????????
- 在c#.net中如何把数据写入临时表?
- ASP.NET
- 在Datagrid的OnDeleteCommand中,单击“删除”后,如何出现“确认”和“取消”按钮???
- DataTable中有500条数据,如何读取中间的第200-300这些数据?
- Microsoft .Net Application Center Test启动后创建一个新测试,录制测试的时候出错
- 一个困惑的问题。
- 如何在asp.net中使IE中的后退、前进按钮失效!!!!!!!!!!
- Gridview中全选的问题
- 本人需用C# 做图片水印 ,请各位大侠帮忙列子,实现相似的效果
if (filename.ToString() != "")
{
string name = filename.Substring(filename.LastIndexOf('.'), filename.Length - filename.LastIndexOf('.'));//获取文件后缀名
string fileurl = Server.MapPath("/mp3/");
if (name.Equals(".mp3") || name.Equals(".wma"))
{
try
{
string fn = DateTime.Now.ToString("yyyyMMddhhmmss");
fn = fn + name; FileUpload1.SaveAs(fileurl + fn);
}
catch (Exception ex)
{ Response.Write("<script> arct('上传失败请重新上传')</script>"); }
}
if (upImage.HasFile)
{
string filePath = "~/upload/" + upImage.FileName;
upImage.SaveAs(MapPath(filePath));
}
/// 在根目录下创建文件夹
///
/// 要创建的文件路径
public void CreateFolder(string FolderPathName)
{
if (FolderPathName.Trim().Length > 0)
{
try
{
string CreatePath = System.Web.HttpContext.Current.Server.MapPath ("work/" + FolderPathName).ToString();
if (!Directory.Exists(CreatePath))
{
Directory.CreateDirectory(CreatePath);
}
}
catch
{
throw;
}
}
} /**/
///
/// 删除一个文件夹下面的字文件夹和文件
///
///
public void DeleteChildFolder(string FolderPathName)
{
if (FolderPathName.Trim().Length > 0)
{
try
{
string CreatePath = System.Web.HttpContext.Current.Server.MapPath (FolderPathName).ToString();
if (Directory.Exists(CreatePath))
{
Directory.Delete(CreatePath, true);
}
}
catch
{
throw;
}
}
} /**/
///
/// 删除一个文件
///
///
public void DeleteFile(string FilePathName)
{
try
{
FileInfo DeleFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString());
DeleFile.Delete();
}
catch
{
}
}
public void CreateFile(string FilePathName)
{
try
{
//创建文件夹
string[] strPath= FilePathName.Split('/');
CreateFolder(FilePathName.Replace("/" + strPath[strPath.Length-1].ToString(),"")); //创建文件夹
FileInfo CreateFile =new FileInfo(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString()); //创建文件
if(!CreateFile.Exists)
{
FileStream FS=CreateFile.Create();
FS.Close();
}
}
catch
{ }
}
/**/
///
/// 删除整个文件夹及其字文件夹和文件
///
///
public void DeleParentFolder(string FolderPathName)
{
try
{
DirectoryInfo DelFolder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("work/" + FolderPathName).ToString());
if (DelFolder.Exists)
{
DelFolder.Delete();
}
}
catch
{
}
}
/**/
///
/// 在文件里追加内容
///
///
public void ReWriteReadinnerText(string FilePathName, string WriteWord)
{
try
{
//建立文件夹和文件
//CreateFolder(FilePathName);
CreateFile(FilePathName);
//得到原来文件的内容
FileStream FileRead = new FileStream(System.Web.HttpContext.Current.Server.MapPath (FilePathName).ToString(), FileMode.Open, FileAccess.ReadWrite);
StreamReader FileReadWord = new StreamReader(FileRead, System.Text.Encoding.Default);
string OldString = FileReadWord.ReadToEnd().ToString();
OldString = OldString + WriteWord;
//把新的内容重新写入
StreamWriter FileWrite = new StreamWriter(FileRead, System.Text.Encoding.Default);
FileWrite.Write(WriteWord);
//关闭
FileWrite.Close();
FileReadWord.Close();
FileRead.Close();
}
catch
{
// throw;
}
} /**/
///
/// 在文件里追加内容
///
///
public string ReaderFileData(string FilePathName)
{
try
{ FileStream FileRead = new FileStream(System.Web.HttpContext.Current.Server.MapPath (FilePathName).ToString(), FileMode.Open, FileAccess.Read);
StreamReader FileReadWord = new StreamReader(FileRead, System.Text.Encoding.Default);
string TxtString = FileReadWord.ReadToEnd().ToString();
//关闭
FileReadWord.Close();
FileRead.Close();
return TxtString;
}
catch
{
throw;
}
}
/**/
///
/// 读取文件夹的文件
///
///
///
public DirectoryInfo checkValidSessionPath(string FilePathName)
{
try
{
DirectoryInfo MainDir = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath (FilePathName));
return MainDir; }
catch
{
throw;
}
}
这是所以有关文件操作的代码,希望能看懂
{
try
{
Upload(FileUpload9);
Upload(FileUpload8);
Upload(FileUpload7);
Upload(FileUpload6);
Upload(FileUpload5);
Upload(FileUpload4);
Upload(FileUpload3);
Upload(FileUpload2);
Upload(FileUpload10);
Upload(FileUpload1); txtmsg.Text = "完成文件上传,如下:<BR>" + ViewState["Upload"].ToString();
ViewState["Upload"] = null;
}
catch (Exception ex)
{
txtmsg.Text = ex.Message;
//txtmsg.Text = "指定文件";
}
StringBuilder sb = new StringBuilder();
sb.Append("上传信息是:<hr color=red>");
}
private void Upload(FileUpload myfileupload)
{
string path = HttpContext.Current.Request.MapPath("~/img/");
if (myfileupload.HasFile)
{
try
{
DirectoryInfo dfile = new DirectoryInfo(path);
FileInfo[] file = dfile.GetFiles(myfileupload.FileName);
if (file.Length == 1)
{
txtmsg.Text = "有同名文件夹";
}
else
{
myfileupload.SaveAs(path + myfileupload.FileName);
ViewState["Upload"] += myfileupload.PostedFile.FileName + "</br>";
}
}
catch (Exception ex)
{
txtmsg.Text += ex.Message;
}
}
else
{
if(hasfile().ToString()!=null)
{
txtmsg.Text = "必须指定一个文件!" + hasfile().ToString();
//txtmsg.Text = hasfile().ToString();
}
}
}
private bool hasfile()
{
bool status = false;
status = FileUpload1.HasFile && FileUpload10.HasFile && FileUpload2.HasFile &&
FileUpload3.HasFile && FileUpload4.HasFile && FileUpload5.HasFile &&
FileUpload6.HasFile && FileUpload7.HasFile && FileUpload8.HasFile &&
FileUpload9.HasFile;
return status;
}
这个是批量上传的,去年写的,可能有点老旧···
{
if (fileUpload.HasFile)
{
string savePath = Server.MapPath("~/upload/");
if (!System.IO.Directory.Exists(savePath))
{
System.IO.Directory.CreateDirectory(savePath);
}
savePath = savePath + "\\" + fileUpload.FileName;
fileUpload.SaveAs(savePath);//保存文件
}
}
{
string path = Server.MapPath("~/File/");
//判断上传文件大小
/*限制文件大小在配置文件中httpRuntime 下的maxRequestLength属性,单位字节;
*/
if (fiLoad.PostedFile.ContentLength > 文件长度)
{
labname.Text = "上传文件大小不能超过文件大小";
return;
}
//上传控件中是否有该文件
else if (fiLoad.HasFile)
{
string filename = fiLoad.FileName;
//判断该文件名是否存在
DirectoryInfo di = new DirectoryInfo(path);
path += filename;
if (di.Exists)
{
FileInfo[] diinfo = di.GetFiles();
foreach (FileInfo item in diinfo)
{
if (item.Name == filename)
{
//myUpload()为页面函数,函数中用confirm(),然后再在函数中调用后台DataBD()方法
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "a", "<script >myUpload()</script>");
return;
}
}
fiLoad.SaveAs(path);
labname.Text = "你成功上传文件 “" + filename + "”";
ShowDataBD();
}
else
{
fiLoad.SaveAs(path);
labname.Text = "你成功上传文件 “" + filename + "”";
ShowDataBD();
}
}
else
{
labname.Text = "请选择要上传的文件";
}
}
public string DataBD()
{
string path = Server.MapPath("~/File/");
if (fiLoad.HasFile)
{
string filename = fiLoad.FileName;
path += filename;
fiLoad.SaveAs(path);
return "你成功替换了文件“" + filename + "”";
}
return null;
}