读书人

请问上关于WinForm下传文件的有关问题

发布时间: 2012-09-02 21:00:34 作者: rapoo

请教下关于WinForm上传文件的问题,谢谢!
这个是我在网上找到的一个类 处理上传下载文件的:

C# code
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Xml.Linq;using System.Windows.Forms;using System.IO;using System.Net;public class UploadInfo{    public static bool UploadFile(string localFilePath, string serverFolder, bool reName)    {        string fileNameExt, newFileName, uriString;        if (reName)        {            fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf(".") + 1);            newFileName = DateTime.Now.ToString("yyMMddhhmmss") + fileNameExt;        }        else        {            newFileName = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);        }        if (!serverFolder.EndsWith("/") && !serverFolder.EndsWith("\\"))        {            serverFolder = serverFolder + "/";        }        uriString = serverFolder + newFileName;   //服务器保存路径        /**/        /// 创建WebClient实例        WebClient myWebClient = new WebClient();        myWebClient.Credentials = CredentialCache.DefaultCredentials;        // 要上传的文件        FileStream fs = new FileStream(newFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);        //FileStream fs = new FileStream(newFileName, FileMode.Open, FileAccess.Read);        BinaryReader r = new BinaryReader(fs);        try        {            //使用UploadFile方法可以用下面的格式            //myWebClient.UploadFile(uriString,"PUT",localFilePath);            byte[] postArray = r.ReadBytes((int)fs.Length);            Stream postStream = myWebClient.OpenWrite(uriString, "PUT");            if (postStream.CanWrite)            {                postStream.Write(postArray, 0, postArray.Length);            }            else            {                MessageBox.Show("文件目前不可写!");            }            postStream.Close();        }        catch        {            //MessageBox.Show("文件上传失败,请稍候重试~");            return false;        }        return true;    }    /**/    /// <summary>    /// 下载服务器文件至客户端    /// </summary>    /// <param name="uri">被下载的文件地址</param>    /// <param name="savePath">另存放的目录</param>    public static bool Download(string uri, string savePath)    {        string fileName;  //被下载的文件名        if (uri.IndexOf("\\") > -1)        {            fileName = uri.Substring(uri.LastIndexOf("\\") + 1);        }        else        {            fileName = uri.Substring(uri.LastIndexOf("/") + 1);        }        if (!savePath.EndsWith("/") && !savePath.EndsWith("\\"))        {            savePath = savePath + "/";        }        savePath += fileName;   //另存为的绝对路径+文件名        WebClient client = new WebClient();        try        {            client.DownloadFile(uri, savePath);        }        catch        {            return false;        }        return true;    } }


这个是我调用的部分:

C# code
Boolean flag = UploadInfo.UploadFile(textBox1.Text, "/Uploads/", false);                if (flag)                {                    MessageBox.Show("上传成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);                    textBox1.Text = "";                }                else                {                    MessageBox.Show("文件上传失败!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);                }

现在的问题是:

1,我这个是WinForm形式的,不是asp.net形式的;上传文件之后 查看目录的时候存在这个文件,但是大小为0KB,求解?

2,Boolean flag = UploadInfo.UploadFile(textBox1.Text, "/Uploads/", false); 这里调用的时候 我上传路径写的是


/Uploads/ 上传之后到了D盘的Uploads目录下 也就是说在本地 我知道cs属于客户端程序,我想问下 有没有方法可以在
WinForm里面上传文件到服务器上面?如果可以 那么路径该怎么写呢 谢谢!

[解决办法]
一起做过现在忘了...
我记得我当时是用winform和ASP.NET相结合使用的,
因为只有这样我们就有了服务器地址和药保存文件所在的服务器路径
通过WINFORM获取本地路径,将其传送到ASP.NET的某个上传页面中,然后让ASP.NET去实现隐式上传下载...
这样从表情看起来就好像是用WINFORM实现的....

[解决办法]
http://hi.baidu.com/dqruanjian/item/7f953f488c0a02b9de2a9f78
[解决办法]
http://blog.csdn.net/happy09li/article/details/6952677

读书人网 >C#

热点推荐