C#客户端程序怎么把图片传到服务器呢?
展开全部
前台页面****************************
<!--程序功能:文件上传程序的页面设计-->
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="5-05.aspx.cs" Inherits="_5_05" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
<br />
<br />
<asp:Label ID="Label5" runat="server" Font-Size="Larger" Height="33px" Text="上传成功!!!"
Visible="False" Width="300px"></asp:Label><br />
<asp:Label ID="Label1" runat="server" Height="40px" Width="450px"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Height="46px" Width="450px"></asp:Label><br />
<asp:Label ID="Label3" runat="server" Height="35px" Width="450px"></asp:Label>
<br />
<asp:Label ID="Label4" runat="server" Height="37px" Width="450px"></asp:Label></div>
</form>
</body>
</html>
后台功能******************************************
//程序功能:后台功能代码,实现文件的上传
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 _5_05 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null)
{
FileUpload1.Visible = true;
string strDir = FileUpload1.PostedFile.FileName;
int myPos = strDir.LastIndexOf("\\");
string strFileName = strDir.Substring(myPos);
string strPath = Server.MapPath(".") + strFileName;
this.Label2.Text = "保存路径:";
this.Label2.Text += strPath;
FileUpload1.PostedFile.SaveAs(strPath);
this.Label5.Visible = true;
this.Label1.Text = "文件名称:";
this.Label1.Text += FileUpload1.PostedFile.FileName;
this.Label3.Text = "文件类型:";
this.Label3.Text += FileUpload1.PostedFile.ContentType;
this.Label4.Text = "文件大小:";
this.Label4.Text += FileUpload1.PostedFile.ContentLength.ToString();
}
}
}
<!--程序功能:文件上传程序的页面设计-->
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="5-05.aspx.cs" Inherits="_5_05" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
<br />
<br />
<asp:Label ID="Label5" runat="server" Font-Size="Larger" Height="33px" Text="上传成功!!!"
Visible="False" Width="300px"></asp:Label><br />
<asp:Label ID="Label1" runat="server" Height="40px" Width="450px"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Height="46px" Width="450px"></asp:Label><br />
<asp:Label ID="Label3" runat="server" Height="35px" Width="450px"></asp:Label>
<br />
<asp:Label ID="Label4" runat="server" Height="37px" Width="450px"></asp:Label></div>
</form>
</body>
</html>
后台功能******************************************
//程序功能:后台功能代码,实现文件的上传
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 _5_05 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null)
{
FileUpload1.Visible = true;
string strDir = FileUpload1.PostedFile.FileName;
int myPos = strDir.LastIndexOf("\\");
string strFileName = strDir.Substring(myPos);
string strPath = Server.MapPath(".") + strFileName;
this.Label2.Text = "保存路径:";
this.Label2.Text += strPath;
FileUpload1.PostedFile.SaveAs(strPath);
this.Label5.Visible = true;
this.Label1.Text = "文件名称:";
this.Label1.Text += FileUpload1.PostedFile.FileName;
this.Label3.Text = "文件类型:";
this.Label3.Text += FileUpload1.PostedFile.ContentType;
this.Label4.Text = "文件大小:";
this.Label4.Text += FileUpload1.PostedFile.ContentLength.ToString();
}
}
}
展开全部
你可以把他转化成二进制流直接存到数据库,再让服务器读, 或者直接发送
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//上传
string fullfileName = FileUpload1.PostedFile.FileName.ToString();
string filename = fullfileName.Substring(fullfileName.LastIndexOf("\\") + 1);
string type = (fullfileName.Substring(fullfileName.LastIndexOf(".") + 1)).ToLower();
int fileSize = FileUpload1.PostedFile.ContentLength;
if (FileUpload1.HasFile)
{
if (type == "jpg" || type == "bmp" || type == "gif")
{
if (fileSize < 120000)
{
this.FileUpload1.PostedFile.SaveAs(Server.MapPath("../images") + "\\ftp\\" + filename);
this.Image1.ImageUrl = "/images/ftp/" + filename;
}
else
{
Response.Write("<Script>alert('" + "你选择的图片应小于120KB!" + "')</Script>");
}
}
else
{
Response.Write("<Script>alert('" + "你选择的图片格式错误!" + "')</Script>");
}
}
string fullfileName = FileUpload1.PostedFile.FileName.ToString();
string filename = fullfileName.Substring(fullfileName.LastIndexOf("\\") + 1);
string type = (fullfileName.Substring(fullfileName.LastIndexOf(".") + 1)).ToLower();
int fileSize = FileUpload1.PostedFile.ContentLength;
if (FileUpload1.HasFile)
{
if (type == "jpg" || type == "bmp" || type == "gif")
{
if (fileSize < 120000)
{
this.FileUpload1.PostedFile.SaveAs(Server.MapPath("../images") + "\\ftp\\" + filename);
this.Image1.ImageUrl = "/images/ftp/" + filename;
}
else
{
Response.Write("<Script>alert('" + "你选择的图片应小于120KB!" + "')</Script>");
}
}
else
{
Response.Write("<Script>alert('" + "你选择的图片格式错误!" + "')</Script>");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询