说下我的开发环境,vs2005、sql2005、C#语言、Web开发
第一步:我已经在项目里创建一个文件夹名为:“File”
第二步:我已经在Insert.aspx页拖了一个控件<asp:FileUpload>
问题:我现在想在Insert.aspx页面点控件上的按钮:“浏览...”上传文件,把文件上传到File里,路径存放在sql2005的字段Accessories里。
这样,我的上传就完成了。接下来我需要在LieBiao.aspx页下载文件,把刚才上传的文件下载回来到电脑里,也就是说我在LieBiao.aspx页面上的GridView上显示下载,当我点下载的时候,就提示我下载。下载的原理应该是通过刚才存的那个路径来找文件来源吧?我不知道改怎么做,
这个功能我已经想了三天了,上网找了好久也看不懂代码,郁闷啊,我是刚毕业的学生,从来没接触过上传下载,有会的教教我吧,我现在甚至连代码写到哪都不知道。
第一步:我已经在项目里创建一个文件夹名为:“File”
第二步:我已经在Insert.aspx页拖了一个控件<asp:FileUpload>
问题:我现在想在Insert.aspx页面点控件上的按钮:“浏览...”上传文件,把文件上传到File里,路径存放在sql2005的字段Accessories里。
这样,我的上传就完成了。接下来我需要在LieBiao.aspx页下载文件,把刚才上传的文件下载回来到电脑里,也就是说我在LieBiao.aspx页面上的GridView上显示下载,当我点下载的时候,就提示我下载。下载的原理应该是通过刚才存的那个路径来找文件来源吧?我不知道改怎么做,
这个功能我已经想了三天了,上网找了好久也看不懂代码,郁闷啊,我是刚毕业的学生,从来没接触过上传下载,有会的教教我吧,我现在甚至连代码写到哪都不知道。
using (T_JOB_Access dal = new T_JOB_Access() ) //dal对象
{
model = dal.QueryCondition(" WHERE JOB_ACCESSORY_ID = " + JobAccId); string strFileName = HttpUtility.UrlEncode(model.JOB_ACCESSORY_NAME);//保存的文件名
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;FileName= " + strFileName);
Response.BinaryWrite(model.JOB_ACCESSORY_CONTENT); //JOB_ACCESSORY_CONTENT字段存放文件二进制内容
Response.End();
}
简单的解释就是文件上传之后读取二进制内容直接存到数据库,下载的时候取出整个二进制内容作为文件流返回即可.
string strFileName = HttpUtility.UrlEncode(model.JOB_ACCESSORY_NAME);//保存的文件名
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;FileName= " + strFileName);
Response.BinaryWrite(blCONTENT); //文件二进制内容
Response.End();
blCONTENT 表示从数据库取出的二进制内容,详情查阅"二进制文件读写数据库"
{
string FullFileName = System.Web.HttpContext.Current.Server.MapPath(FileName);
FileInfo DownloadFile = new FileInfo(FullFileName);
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End(); }
/// 上传文件 返回空为没有文件,返回0为格式错误!
/// </summary>
/// <returns></returns>
private string UploadFile()
{
string directoryPath = "";
string folderDate = "";
string month = "";
string day = "";
string fileName = "";
string fileNameLast = "";
string saveAddr = "";
if (DateTime.Now.Month < 10)
{ month = "0" + DateTime.Now.Month; }
else
{ month = Convert.ToString(DateTime.Now.Month); } if (DateTime.Now.Day < 10)
{ day = "0" + DateTime.Now.Day; }
else
{ day = Convert.ToString(DateTime.Now.Day); }
folderDate = DateTime.Now.Year + "" + month + "" + day; if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
{
fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName); fileNameLast = fileName.Substring(fileName.Length - 4, 4).ToLowerInvariant();
if (!fileNameLast.Contains("zip") && !fileNameLast.Contains("txt") && !fileNameLast.Contains("rar") && !fileNameLast.Contains("doc"))
{
return "0";//文件格式错误!
}
Random r = new Random();
fileName = Convert.ToString(r.Next(10000000, 99999999)) + fileNameLast; directoryPath = Server.MapPath("../file/" + folderDate + "/");
if (Directory.Exists(directoryPath))//文件夹是否存在
{
saveAddr = Server.MapPath("../file/" + folderDate + "/") + fileName;
FileUpload1.PostedFile.SaveAs(saveAddr); }
else
{
Directory.CreateDirectory(directoryPath);
saveAddr = Server.MapPath("../file/" + folderDate + "/") + fileName;
FileUpload1.PostedFile.SaveAs(saveAddr);
}
}
else
{
return "";
}
return "file/" + folderDate + "/" + fileName;
}
<httpRuntime maxRequestLength="200000" executionTimeout="6000"/>
-----------------------------------
http://www.jiemengwu.com/ 解梦屋 http://www.phpzy.com/php/ 绿色php资源
//这个方法用于判断大小的
private bool IsUploadSizeFit(FileUpload fu)
{
const int maxFileSize = 4000000;
int x = fu.PostedFile.ContentLength;
if (fu.PostedFile.ContentLength > maxFileSize) return false;
return true;
}
//按钮,提交上传.有些参数你用不到的.大体就是获取文件名,输入文件路径.
protected void sumbt_Click1(object sender, EventArgs e)
{
if (Name.Text.ToString().Trim() == "")
{
Response.Write("<script>alert(' 提示:文件名称不能为空!')</script>");
}
else
{ if (FileUpload1.HasFile)
{
string FileType = FileUpload1.PostedFile.ContentType;
string FileName = FileUpload1.FileName;
FileInfo file = new FileInfo(FileName);
string fileName = file.Name;
//XX是你上传的路径
string webFilePath = Server.MapPath("~/XX/" + fileName); // 服务器端文件路径 try
{
if (!IsUploadSizeFit(FileUpload1))
{
Response.Write("<script>alert(' 提示:文件大小超过允许范围!')</script>"); }
else
{
FileUpload1.SaveAs(webFilePath);
Response.Write("<script>alert(' 文件上传成功!')</script>");
Response.Redirect("DownLoad.aspx");
} }
catch (Exception ex)
{
Response.Write("<script>alert('error ')</script>");
} } }
Response.AppendHeader("Content-Disposition", "attachment; filename="+HttpUtility.UrlEncode(FileID,System.Text.Encoding.UTF8));
Response.TransmitFile(Server.MapPath("~/XX/" + FileName));
Response.End();
web fileupload,还有很多组件
FileInfo Fi = new FileInfo(filePath);
if (Fi.Exists)
{
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=1.excel");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
string path = Server.MapPath("~/") + "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(obj.Name, System.Text.Encoding.GetEncoding("utf-8")));
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();
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);
}