欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » C# » 正文

热门阅读

在WinForm中利用WebService上传图片到服务器

创建时间:2012年02月17日 17:21  阅读次数:(21863)
分享到:
因为WinForm都是运行在本地的,而我们的网站一般都是布署在服务器上,运行在服务器上的,所以在网站上面上传文件,就好似于保存文件到本地。但在WinForm上就不一样了,本章我们简单举一个在WinForm利用WebService上传文件到服务器的方法:

首先们先创建一个WebService服务,该服务包含一个UpdateFile方法,该方法接收两个byte[]与string类型参数。该方法非常简单,就是按照string参数指定的路径和名称将byte[]参数值保存到服务器,代码如下:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService : System.Web.Services.WebService

[WebMethod]
public bool UpdateFile(byte[] content, string pathandname)
{
File.WriteAllBytes(Server.MapPath(pathandname), content);
}
}

为了安全,我们可以验证一下pathandname的值,使其只保存图片格式的文件。全部代码如下:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService : System.Web.Services.WebService
{
    [WebMethod]
    public bool UpdateFile(byte[] content, string pathandname)
    {
        int index = pathandname.LastIndexOf(".");
        if (index == 0)
        {
            return false;
        }
        else
        {
            string extended = string.Empty;
            if (index + 1 == pathandname.Length)
            {
                return false;
            }
            else
            {
                extended = pathandname.Substring(index + 1);
                if (extended == "jpeg" || extended == "gif" || extended == "jpg")
                {
                    try
                    {
                        File.WriteAllBytes(Server.MapPath(pathandname), content);
                        return true;
                    }
                    catch (Exception ex)
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }
    }
}

好了,创建完WebService后,将它布署到服务器上面,然后在Winform中添加对该服务的引用,添加方法如下:
在winform项目的引用-添加服务引用,在打开的对话框的地址栏中添加布署好的WebService地址,点击前往,验证通过后即添加成功了。如下图:


然后,我们就可以在应用程序中使用它了,为了安全,我们在winform中再次验证上传文件只可以为图片。代码如下:
private void button11_Click(object sender, EventArgs e)
{
    OpenFileDialog fileDialog = new OpenFileDialog();

    if (fileDialog.ShowDialog() == DialogResult.OK)
    {
        string extension = Path.GetExtension(fileDialog.FileName);
        string[] str = new string[] { ".gif", ".jpge", ".jpg" };
        if (!str.Contains(extension))
        {
            MessageBox.Show("仅能上传gif,jpge,jpg格式的图片!");
        }
        else
        {
            FileInfo fileInfo = new FileInfo(fileDialog.FileName);
            if (fileInfo.Length  > 20480)
            {
                MessageBox.Show("上传的图片不能大于20K");
            }
            else
            {
                Stream file = fileDialog.OpenFile();

                byte[] bytes = new byte[file.Length];
                file.Read(bytes, 0, bytes.Length);

                //实例化WebService服务。ServiceReference1是我们在添加引用时设置的命名空间
                ServiceReference1.WebServiceSoapClient WebServiceSoapClient = new AutoPage.ServiceReference1.WebServiceSoapClient();

                DateTime time = DateTime.Now;
                //重命名图片的名称与路径
                string pathandname = "/images/" + time.ToString("yyyyMMddHHmmss") + extension;
                if (WebServiceSoapClient.UpdateFile(bytes, pathandname))
                {
                    MessageBox.Show("上传成功!");
                    this.textBox1.Text = pathandname;
                }
                else
                {
                    MessageBox.Show("上传失败!");
                }
            }
        }
    }
}


本示例代码仅为实现该功能,比较简单,仅供大家参考!
来源:.net学习网
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论2条
  • #1楼  评论人:匿名  评论时间:2012-2-22 9:26:08
  • 学习了,谢谢!
  • #2楼  评论人:伞下的雨  评论时间:2012-6-5 23:35:35
  • .NET 利用webService上传图片至服务器 http://user.qzone.qq.com/669247240/blog/1338887763
发表评论:
留言人:
内  容:
请输入问题 12+38=? 的结果(结果是:50)
结  果: