asp.net 文件上传代码

本文介绍了一个使用ASP.NET处理文件上传的实例,包括文件保存、数据库插入及页面跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一般处理程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;

namespace web
{
    /// <summary>
    /// iputDecoment 的摘要说明
    /// </summary>
    public class iputDecoment : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            String Name = context.Request["docName"];
            String Nsg = context.Request["DocMsg"];
            HttpPostedFile docWords = context.Request.Files["Decoment"];
            string filename = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + Path.GetExtension(docWords.FileName);//有bug的,一毫秒内多个人上传多个文件 
            string path = context.Server.MapPath("~/Doc/" + filename);
            docWords.SaveAs(path);
            
            context.Response.Write("Hello World");
            //SqlHelper.ExecuteNonQuery("Insert into T_Products(Name,CategoryId,ImagePath,Msg) values(@Name,@CategoryId,@ImagePath,@Msg)", new SqlParameter("@Name", name), new SqlParameter("@CategoryId", categoryId), new SqlParameter("@ImagePath", "/uploadfile/" + filename), new SqlParameter("@Msg", msg));
            //context.Response.Redirect("ProductList.ashx");


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

HTML文件


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">


<head>
    <title>文件上传</title>
</head>
<body>
<form action="../iputDecoment.ashx" method="post" enctype="multipart/form-data">

    <h1>文件上传</h1> <br />

    文件名:<input type="text" id="DocName" name="docName" "/><br />

    上传文件:<input type="file" name="Decoment" /><br />

    文件描述:<br />
    <textarea name="DocMsg" cols="20" rows="5"></textarea>
    <input type="submit"  value="提交" name="Save" />
     <input type="reset"  value="重置" name="Save" />
     </form>
</body>

</html>
需注意的是必须记得加入:
enctype="multipart/form-data"
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px; color: red;">enctype="multipart/form-data"是上传二进制数据; form里面的input的值以2进制的方式传过去。 </span><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;">form里面的input的值以2进制的方式传过去,所以request就得不到值了。</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值