C#文件上传与下载的实现方法
程序员文章站
2023-12-15 21:17:28
本文实例为大家分享了c#实现文件上传与下载的具体代码,供大家参考,具体内容如下
c#实现文件上传代码:
public actionresult uploa...
本文实例为大家分享了c#实现文件上传与下载的具体代码,供大家参考,具体内容如下
c#实现文件上传代码:
public actionresult upload() { // var pathurl = "http://" + request.url.authority; var file = request.files["filedata"]; var uploadfilename = file.filename; string filepath = "/file/" + uploadfilename; string absolutepath = server.mappath(filepath); file.saveas(absolutepath); //将上传的东西保存 return json(new { filename = uploadfilename, filepath = filepath }); }
c#实现文件下载功能:
public actionresult download(string filename) { string filename = filename;//客户端保存的文件名 string filepath = server.mappath("/file/"+ filename);//路径 //以字符流的形式下载文件 filestream fs = new filestream(filepath, filemode.open); byte[] bytes = new byte[(int)fs.length]; fs.read(bytes, 0, bytes.length); fs.close(); response.contenttype = "application/octet-stream"; //通知浏览器下载文件而不是打开 response.addheader("content-disposition", "attachment; filename=" + httputility.urlencode(filename, system.text.encoding.utf8)); response.binarywrite(bytes); response.flush(); response.end(); return json(""); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。