C#实现文件上传及文件下载功能实例代码
程序员文章站
2023-12-16 13:05:22
废话不多说了,直接给大家贴代码了,具体代码如下所示:
public actionresult upload()
{
// var path...
废话不多说了,直接给大家贴代码了,具体代码如下所示:
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 }); }
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(""); }
总结
以上所述是小编给大家介绍的c#实现文件上传及文件下载功能实例代码,希望对大家有所帮助