jQuery上传文件和Core Web API接收保存文件

本文介绍如何利用jQuery实现文件上传,并详细阐述ASP.NET Core Web API如何接收并保存上传的文件,提供了相关的代码示例和参考资料。

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

jQuery上传文件和API接收保存文件
- jQuery代码是:

        <input type="file" id="files" name="files" multiple />
        <input type="button"  id="upload" value="Upload Selected Files" />

        $("#upload").click(function (evt) {
            var fileUpload = $("#files").get(0);
            var files = fileUpload.files;
            var data = new FormData();
            console.log("length=" + files.length);
            for (var i = 0; i < files.length ; i++) {
                data.append(files[i].name, files[i]);
            }
            data.append("1111", "222");
            $.ajax({
                type: "POST",
                url: "http://localhost:50090/api/test",
                contentType:false,
                dateType:"json",
                processData: false,
                data: data,
                success: function (json) {
                    alert(json);
                    var list = JSON.parse(json);
                    alert(list.res);
                },
                error: function () {
                    alert("There was error uploading files!");
                }
            });
        });
  • API代码是:

        string path = "";
        long size = 0;
        int i = 0;
        var files = HttpContext.Request.Form.Files;
        if (files.Count > 0)
        {
            //可以写遍历files
            var file = files[0];
            string upload_path = Directory.GetCurrentDirectory() + "/wwwroot";
            DateTime now = DateTime.Now;
            if (Directory.Exists(upload_path + "/images/" + now.Year) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(upload_path + "/images/" + now.Year);
            }
            if (Directory.Exists(upload_path + "/images/" + now.Year + "/" + now.ToString("MMdd")) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(upload_path + "/images/" + now.Year + "/" + now.ToString("MMdd"));
            }
            upload_path = upload_path + "/images/" + now.Year + "/" + now.ToString("MMdd");//新的目录
    
            var filename = ContentDispositionHeaderValue
                            .Parse(file.ContentDisposition)
                            .FileName
                            .Trim('"');
            string houzhui = filename.Substring(filename.IndexOf("."));
            var filenamenew = DateTime.Now.ToString("yyyyMMddHHmmssfff") + houzhui;
            filename = upload_path+"/" + filenamenew;
            path= "/images/" + now.Year + "/" + now.ToString("MMdd")+"/"+ filenamenew;
            size += file.Length;
            using (FileStream fs = System.IO.File.Create(filename))
            {
                file.CopyTo(fs);
                fs.Flush();
                i += 1;
            }
        }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值