BootStrap_FileInput配置file按钮uploadUrl上传PDF文件( 前端+后端接口代码)

本文介绍了如何在ASP.NET中使用FileUploadHandler类处理文件上传请求,包括文件类型限制、上传路径创建、错误处理以及响应返回。主要关注服务器端文件操作和HTTPPOST接口的实现。

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

 <div class="item form-group">

     <div class="col-sm-12 col-md-12 col-xs-12 ">
         <div class="compose-textarea">
             <div class="file-loading">
                 <input id="File" class="file" type="file" />
             </div>
         </div>
     </div>
 </div>
 $('#File').fileinput('refresh', {
     uploadUrl: "FileUploadHandler.ashx?upload=Instruction",
     allowedFileExtensions: ['pdf'],//允许上传文件类型
 }).on("fileuploaded", function (event, data) {
     //上传后执行的改变
 });
public class FileUploadHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private static string path;
    public System.Collections.ArrayList arrExtensions = new System.Collections.ArrayList();
    public void ProcessRequest(HttpContext context)
    {

        //new LogFile<FileUploadHandler>().Error("###### Excute Upload DBUpgrade");
        Array FileExtensions = GetExtensions();

        const int maxSize = 1500000;

        try
        {
            BPMSServerConfigControl server = new BPMSServerConfigControl();
            string filenameStr = context.Request.Form["filename"];
            if (context.Request.QueryString["upload"] == "Instruction")
            {
                UploadResponse resp = new UploadResponse();
              
                string fname ="1.pdf";//文件名称
                string savePath = AppDomain.CurrentDomain.BaseDirectory + "PDF\\"+ fname;//文件路径
                if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "PDF"))    //判断文件存放路径是否存在
                {
                    Directory.CreateDirectory(savePath);
                }
                // (1)获取上传的文件,保存到 应用程序基目录
                HttpPostedFile postedFile = context.Request.Files[0];
                postedFile.SaveAs(savePath);
               
                context.Response.ContentType = "text/html";
                context.Response.Write(new JavaScriptSerializer().Serialize(resp));
            }

        }
        catch (Exception ex)
        {
            
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值