BlogYi 上传文件和图片的bug

源代码:

位于:PrecompiledWeb\BlogEngine.NET\admin\Pages\Add_entry.aspx.cs中

145行:这个是上传保存的地址。

private void Upload(string virtualFolder, FileUpload control, string fileName)
	{
		string folder = Server.MapPath(virtualFolder);
		if (!Directory.Exists(folder))
			Directory.CreateDirectory(folder);

		control.PostedFile.SaveAs(folder + fileName);
	}

119行:这个是上传文档并且在 中加人 文档地址和图片地址的地方

	 	private void btnUploadImage_Click(object sender, EventArgs e)
	{
		string relativeFolder = DateTime.Now.Year.ToString() + Path.DirectorySeparatorChar + DateTime.Now.Month.ToString() + Path.DirectorySeparatorChar;
		string folder = BlogSettings.Instance.StorageLocation + "files" + Path.DirectorySeparatorChar;
		string fileName = txtUploadImage.FileName;
		Upload(folder + relativeFolder, txtUploadImage, fileName);

		string path = Utils.RelativeWebRoot.ToString();
		string img = string.Format("<img src=\"{0}image.axd?picture={1}\" alt=\"\" />", path, Server.UrlEncode(relativeFolder.Replace("\\", "/") + fileName));
		txtContent.Text += img;
		txtRawContent.Text += img;
	}

	private void btnUploadFile_Click(object sender, EventArgs e)
	{
		string relativeFolder = DateTime.Now.Year.ToString() + Path.DirectorySeparatorChar + DateTime.Now.Month.ToString() + Path.DirectorySeparatorChar;
		string folder = BlogSettings.Instance.StorageLocation + "files" + Path.DirectorySeparatorChar;
		string fileName = txtUploadFile.FileName;
		Upload(folder + relativeFolder, txtUploadFile, fileName);

		string a = "<p><a href=\"{0}file.axd?file={1}\">{2}</a></p>";
		string text = txtUploadFile.FileName + " (" + SizeFormat(txtUploadFile.FileBytes.Length, "N") + ")";
		txtContent.Text += string.Format(a, Utils.RelativeWebRoot, Server.UrlEncode(relativeFolder.Replace("\\", "/") + fileName), text);
		txtRawContent.Text += string.Format(a, Utils.RelativeWebRoot, Server.UrlEncode(relativeFolder.Replace("\\", "/") + fileName), text);
	}	

源代码的问题上传同名文件的时候使用的是原来文件的地址,,,很郁闷的。。。

修改:防止同名文件,,其实这里我也可以做个判断。。但是那样 如果有一直存在哪不是很费服务器,,随便了那样也可以的。。。

private string Upload(string virtualFolder, FileUpload control, string fileName)
    {
        string folder = Server.MapPath(virtualFolder);
        if (!Directory.Exists(folder))
        Directory.CreateDirectory(folder);
        string dangqiannanme = DateTime.Now.ToString("yyyy-MM-dd-ss") + fileName;
        string savapath = folder + dangqiannanme;
         control.PostedFile.SaveAs(savapath);
         return dangqiannanme;
    } 

private void btnUploadImage_Click(object sender, EventArgs e)
	{
		string relativeFolder = DateTime.Now.Year.ToString() + Path.DirectorySeparatorChar + DateTime.Now.Month.ToString() + Path.DirectorySeparatorChar;
		string folder = BlogSettings.Instance.StorageLocation + "files" + Path.DirectorySeparatorChar;
		string fileName = txtUploadImage.FileName;
		string  savaphname=Upload(folder + relativeFolder, txtUploadImage, fileName);

		string path = Utils.RelativeWebRoot.ToString();
        string img = string.Format("<img src=\"{0}image.axd?picture={1}\" alt=\"\" />", path, Server.UrlEncode(relativeFolder.Replace("\\", "/") + savaphname));
		txtContent.Text += img;
		txtRawContent.Text += img;
	}

	private void btnUploadFile_Click(object sender, EventArgs e)
	{
		string relativeFolder = DateTime.Now.Year.ToString() + Path.DirectorySeparatorChar + DateTime.Now.Month.ToString() + Path.DirectorySeparatorChar;
		string folder = BlogSettings.Instance.StorageLocation + "files" + Path.DirectorySeparatorChar;
		string fileName = txtUploadFile.FileName;
		string  savaphname=Upload(folder + relativeFolder, txtUploadFile, fileName);

		string a = "<p><a href=\"{0}file.axd?file={1}\">{2}</a></p>";
		string text = txtUploadFile.FileName + " (" + SizeFormat(txtUploadFile.FileBytes.Length, "N") + ")";
        txtContent.Text += string.Format(a, Utils.RelativeWebRoot, Server.UrlEncode(relativeFolder.Replace("\\", "/") + savaphname), text);
        txtRawContent.Text += string.Format(a, Utils.RelativeWebRoot, Server.UrlEncode(relativeFolder.Replace("\\", "/") + savaphname), text);
	}

这样基本上就可以防止同名文件上传的文件了。。。

posted @ 2011-03-01 20:15  王大湿  阅读(246)  评论(0编辑  收藏  举报