C#使用HTML文件中的file文件上传,用C#代码接收上传文件
<form><inputid="fileUpLoad"name="fileUpLoad"type="file"/><inputid="submit"name="submi...
<form>
<input id="fileUpLoad" name="fileUpLoad" type="file"/>
<input id="submit" name="submit" type="submit"/>
</form>
C#使用HTML文件中的file文件上传,用C#代码接收上传文件 展开
<input id="fileUpLoad" name="fileUpLoad" type="file"/>
<input id="submit" name="submit" type="submit"/>
</form>
C#使用HTML文件中的file文件上传,用C#代码接收上传文件 展开
12个回答
展开全部
<form id="form1" method="post" enctype="multipart/form-data" action="test.aspx">
<input id="File1" type="file" name="File1"/>
<input id="Submit1" type="submit" value="submit" />
</form>
c# 代码 test.aspx.cs后台代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Files.Count>0)
{
HttpPostedFile f = Request.Files[0];
f.SaveAs(Server.MapPath("test.dat"));
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1、C#实现图片上传代码如下
//图片上传
protected void Bt_save_Click(object sender, EventArgs e)
{
//上传
if (Upload_image.HasFile)
{
string savePath = Server.MapPath("~/upload/");//指定上传文件在服务器上的保存路径
//检查服务器上是否存在这个物理路径,如果不存在则创建
if (!System.IO.Directory.Exists(savePath))
{
System.IO.Directory.CreateDirectory(savePath);
}
//未知原因 upload.file 在上传图片后变成未选定 - 设置它已经选定了
string ext = System.IO.Path.GetExtension(Upload_image.FileName);
if (ext == ".jpg" || ext == ".png")
{ //图片重命名,userid+getdatetime
string time = DateTime.Now.ToString("yyyyMMddhhmmss");
string userid = Session["userid"].ToString();
string newname = "eshop" + userid + time + ext;
savePath = savePath + "\\" + newname;
Upload_image.SaveAs(savePath);
//路径存session ,稍后加入数据库
Session.Remove("imageurl");//新上传清空已经存的
Session.Add("imageurl", "~/upload/" + newname);
Lb_image.Text = "已选择" + Upload_image.FileName;
this.Image_goods.ImageUrl = "~/upload/" + newname;
}
else { Lb_image.Text = "图片仅支持JPG、png格式!"; }
}
else
{
Lb_image.Text = "没有选择图片!";
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
前端代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUpLoad.aspx.cs" Inherits="DEMO.FileUpLoad" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Image ID="Image1" runat="server"/>
<asp:Button ID="btnUpLoad" runat="server" Text="上传" OnClick="btnUpLoad_Click" />
<asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label>
</div>
</form>
</body>
</html>
后端代码
//string serverpath = Server.MapPath("~/ImageFile");
if (FileUpload1.PostedFile.ContentLength > 0)
{
if (File.Exists(@"C:\Users\WJM\documents\visual studio 2013\Projects\DEMO\DEMO\ServerImages\" + FileUpload1.PostedFile.FileName))
{
Label1.Text = "文件已经存在";
}
else
{
FileUpload1.PostedFile.SaveAs(@"C:\Users\WJM\documents\visual studio 2013\Projects\DEMO\DEMO\ServerImages\" + FileUpload1.PostedFile.FileName);
this.Image1.ImageUrl = this.Image1.ImageUrl = @"ServerImages/"+FileUpload1.PostedFile.FileName;//相对路径,将上传的图片给Image控件;//相对路径,将上传的图片给Image控件
Label1.Text = "上传成功!";
}
}
else
{
Label1.Text = "上传失败";
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//HTML页面
<div>
<label>文件:</label> <input id="file" type="file" />
<button id="btnUpload">上传</button>
</div>
<script>
$("#btnUpload").on("click", function () {
var formdata = new FormData();
var file = $("#file").get(0).files[0];
formdata.append("file1", file);
$.ajax({
type:"post",
url: "Upload",//url地址
contentType: false,
cache: false,
processData: false,
data: formdata,
success: function (data) {
console.log(data);
}
});
});
</script>
//MVC的Action
[HttpPost]
public ActionResult Upload()
{
var files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
var file = files[i];
//To Do
//........
}
return View();
}
如果有需要的话可以联系我 我这边又一个简单的demo
<div>
<label>文件:</label> <input id="file" type="file" />
<button id="btnUpload">上传</button>
</div>
<script>
$("#btnUpload").on("click", function () {
var formdata = new FormData();
var file = $("#file").get(0).files[0];
formdata.append("file1", file);
$.ajax({
type:"post",
url: "Upload",//url地址
contentType: false,
cache: false,
processData: false,
data: formdata,
success: function (data) {
console.log(data);
}
});
});
</script>
//MVC的Action
[HttpPost]
public ActionResult Upload()
{
var files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
var file = files[i];
//To Do
//........
}
return View();
}
如果有需要的话可以联系我 我这边又一个简单的demo
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询