`
wsql
  • 浏览: 11812699 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

文件上传和下载

 
阅读更多

最近在做一个信息发布系统。要有上传文件和下载的功能

直接上源码:

前台

<script language="JavaScript" type="text/javascript" >
function addFile()
{
var str = '<BR><INPUT type="file" size="50" NAME="File" runat="server">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}

table align="center" class="fullwidth">
<tr>
<td style="width: 108px; height: 15px">
附件:</td>
<td style="width: 448px; height: 15px;">
<p id="MyFile">
<input id="filMyFile" name="filMyFile" size="50" type="file" />
</p>
</td>
<td style="width: 448px; height: 15px">
<input id="btnadd" onclick="addFile()" type="button" value="Add" visible="false" disabled="disabled" />提示:长传的附件不能大于6M</td>
</tr>
<tr>
<td style="width: 108px; height: 15px">
文件名称:</td>
<td style="width: 448px; height: 15px">
<asp:TextBox ID="txt_filename" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txt_filename"
ErrorMessage="请输入文件名称"></asp:RequiredFieldValidator></td>
<td style="width: 448px; height: 15px">
</td>
</tr>
<tr>
<td style="width: 108px; height: 15px">
上传作者:</td>
<td style="width: 448px; height: 15px">
<asp:TextBox ID="txt_username" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_username"
ErrorMessage="请输入你的大名"></asp:RequiredFieldValidator></td>
<td style="width: 448px; height: 15px">
</td>
</tr>
<tr>
<td style="width: 108px; height: 15px">
备注信息:</td>
<td style="width: 448px; height: 15px">
<asp:TextBox ID="txt_remark" runat="server" Height="98px" TextMode="MultiLine" Width="306px"></asp:TextBox></td>
<td style="width: 448px; height: 15px">
</td>
</tr>
<tr>
<td style="width: 108px">
</td>
<td style="width: 448px">
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上传" CssClass="btn" />
<asp:Label ID="lblAttachmentError" runat="server" ForeColor="Red"></asp:Label>
<asp:Label
ID="lblAttachment" runat="server"></asp:Label></td>
<td style="width: 448px">
</td>
</tr>
</table>

后台:

protected void btnUpload_Click(object sender, EventArgs e)
{

HttpFileCollection files = HttpContext.Current.Request.Files;
for (int i = 0; i < files.Count; i++)
{
if (i < files.Count && i < 10)
{
if (files[i].FileName != "" || files[i] != null)
{
int FileSize = 6 * 1024 * 1024;
HttpPostedFile myFile = files[i];
string strFilePath = myFile.FileName.ToString().Trim();
this.lblAttachmentError.Text = "<" + strFilePath + ">"; // Show file name
int nFindSlashPos = strFilePath.Trim().LastIndexOf("//") + 1;
string UploadFileName = strFilePath.Substring(nFindSlashPos);
string FileName =string.Format("{0:yyMMdd-hhmmss}", DateTime.Now) + "_" + UploadFileName; //this.txtWorkOrder.Text

if (myFile.FileName.Trim() == "") // Empty value in Browse Box
{
this.lblAttachmentError.Text = "没有文件被选择";
return;
}

if (myFile.ContentLength != 0)
{
if (myFile.ContentLength > FileSize)
{
this.lblAttachmentError.Text = "文件大小限制在6M以下";
return;
}
this.lblAttachment.Text += "<BR>" + FileName;
this.lblAttachmentError.Text = "";

myFile.SaveAs(this.Request.PhysicalApplicationPath.ToString().Trim() + @"/uploads/" + FileName);

//保存代码
string filename = string.Empty;
int filesize;
string filepath = string.Empty;
string uploaduser = string.Empty;
string remark = string.Empty;

filename = txt_filename.Text.Trim();
filesize = myFile.ContentLength;
filepath = FileName;
uploaduser = txt_username.Text.Trim();
remark = txt_remark.Text.Trim();

string strsql = string.Format("insert onlinedoc(filename,filesize,path,uploaduser,remark)values('{0}',{1},'{2}','{3}','{4}')",filename,filesize,filepath,uploaduser,remark);
DABaseAccess db = new DABaseAccess();
if (db.GetNonQuery(strsql.ToString()))
{
JScript.Alert("恭喜你文件上传成功,保存数据库成功!", this.Page);
}
else
{
JScript.Alert("保存数据库失败,请与管理员龚德辉联系!", this.Page);
}


}
else
{
this.lblAttachmentError.Text = "文件没有找到";
return;
}
}
}
else
this.lblAttachmentError.Text = "Uploaded File exceed limits.";
}

}

这个可以同时上传N个 不过我只传一个。

下载:

protected void btndownload_Click(object sender, ImageClickEventArgs e)
{

string path = Server.MapPath("uploads/") + this.path; // Session["file"].ToString();
//初始化 FileInfo 类的实例,它作为文件路径的包装
FileInfo fi = new FileInfo(path);
//判断文件是否存在
if (fi.Exists)
{
//将文件保存到本机上
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(fi.Name)));
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(fi.FullName);
Response.End();
}

}

编码采用UTF-8所以标题不会乱码

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics