asp.net上传文件

第一种方法

前端:

<div>

单文件上传

<form enctype="multipart/form-data" method="post" action="upload.aspx">

<input type="file" name="files" />

<input type="submit" value="上传" />

</form>

</div>

后端:

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class upload_upload : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

HttpContext context = HttpContext.Current;

context.Response.ContentType = "text/plain";

//获取所有的上传文件

//如果上传的文件超过了4M,则会报异常超过了最大请求长度。,需要在Web.config配置

HttpFileCollection hfc = context.Request.Files;

//如果是hfc.Count为0,则可以认为是直接请求该ashx处理程序

for (int i = 0; i < hfc.Count; i++)

{

HttpPostedFile hpf = hfci;

if (hpf.ContentLength > 0)

{

//大小限制

int maxSize = 2 * 1024 * 1024; //最大是2M(转换为字节)

if (hpf.ContentLength > maxSize)

{

context.Response.Write("上传文件大小超出限制");

return;

}

//扩展名限制

string\[\] exts = { "image/jpg", "image/jpeg", "image/gif", "image/png" };

if (!exts.Contains(hpf.ContentType.ToLower()))

{

context.Response.Write("必须上传图片文件");

return;

}

string fileName = Path.GetFileName(hpf.FileName);

hpf.SaveAs(context.Server.MapPath("~/upload/" + fileName));

}

}

获取指定name的文件上传

该方式如果是html5的<input type="file" name="files" multiple />则始终是获取第一个上传的文件,multiple

是支持多个文件上传,所以如果上传文件多于一个,则会丢失

//HttpPostedFile hpf = context.Request.Files"files";

//if (hpf!= null && hpf.ContentLength > 0)

//{

// hpf.SaveAs(context.Server.MapPath("~/UpLoad/" + Path.GetFileName(hpf.FileName)));

//}

context.Response.Write("上传成功");

}

}

第二种方法

前端(1)

<div id="divImg">

<input type="file" class="file" name="file" multiple="multiple" id="file"/>

<button type="button" οnclick="uploadFile()">上传</button>

</div>

js

function uploadFile(){

let files = $("#file").prop("files");

let formData = new FormData();

// 对每个文件进行循环处理

for (let i = 0; i < files.length; i++) {

let file = filesi;

// 添加文件到formData

formData.append('files'+i+'', file, file.name);

}

$.ajax({

url: ctx + "/file/upload",

type: 'POST',

data: formData,

processData: false,

contentType: false,

async: false,

cache: false,

success: function(data) {

console.log(data);

}

});

}

前端(2)

js

<form id="fileUploadForm" enctype="multipart/form-data">

<input type="file" id="fileInput">

<button type="submit">上传</button>

</form>

<script>

$(document).ready(function() {

$('#fileUploadForm').submit(function(e) {

e.preventDefault();

var fileInput = document.getElementById('fileInput');

var file = fileInput.files0;

var reader = new FileReader();

reader.onload = function(e) {

var fileData = e.target.result;

var formData = new FormData();

formData.append('file', file);

$.ajax({

url: 'upload.php',

type: 'POST',

data: formData,

processData: false,

contentType: false,

success: function(response) {

console.log(response);

},

error: function(xhr, status, error) {

console.error(error);

}

});

};

后端同每一种方法

相关推荐
用户835629078051几秒前
如何使用 Python 在 PowerPoint 演示文稿中添加漏斗图
后端
ServBay6 分钟前
Qwen3.7-Max 发布,全能智能体基座
后端·aigc·ai编程
KeepPush27 分钟前
Python JSON 完全指南:从基础到实战,掌握数据交换核心技能
后端
Cosolar39 分钟前
深度测评 | QoderWork:当 AI 不再只是"聊天搭子",而是真能帮你干活的桌面智能体
人工智能·后端·程序员
前端Hardy40 分钟前
21.8 万周下载!这个 React 表格组件,10 行代码就能跑起来
前端·javascript·后端
用户8356290780511 小时前
使用 Python 在 Word 文档中添加和管理脚注
后端
字节跳动数据库1 小时前
一个请求稳定的一生
后端·程序员
RainCity1 小时前
Java Swing 自定义组件库分享(十一)
java·笔记·后端
掘金一周2 小时前
问卷调查:如果现在收到裁员通知,你手里的现金流能支撑多久? | 沸点周刊6.4
前端·人工智能·后端
JustHappy3 小时前
古法编程秘籍(四):函数究竟是什么?把函数最重要的能力一次讲清楚
前端·后端·面试