我想将图片文件从客户端上传到服务器上,我的是winform,
我将文件从本地上传到服务器时,首先用file.copy的方法,但只能本局域网内部上传,因要进行外部上传所以我改用下面的方法,但还是不能上传,但也没有报错,请大家帮忙找下原因 private void btn_save_Click(object sender, EventArgs e)
{
NetworkCredential credentials =new NetworkCredential("administrator", "ISAweb");
topf.DemonstrateWebClient(txt_fileroad.Text, credentials);
}public void DemonstrateWebClient(string sourcestr, NetworkCredential credentials)
{
WebClient webClient = new WebClient();
webClient.Credentials = credentials; // Upload file.
try
{
string fileName = sourcestr.Substring(sourcestr.LastIndexOf("\\") + 1, sourcestr.Length - sourcestr.LastIndexOf("\\") - 1); //ReadFileName("upload");
//string uploadAddress = serverAddress + fileName;
Uri address1 = new Uri("http://www.toploria.com.cn/TKservice/Pictures/");
//Console.WriteLine("Uploading file " + uploadAddress);
webClient.UploadFile(address1, fileName);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
} webClient.Dispose();
}
我将文件从本地上传到服务器时,首先用file.copy的方法,但只能本局域网内部上传,因要进行外部上传所以我改用下面的方法,但还是不能上传,但也没有报错,请大家帮忙找下原因 private void btn_save_Click(object sender, EventArgs e)
{
NetworkCredential credentials =new NetworkCredential("administrator", "ISAweb");
topf.DemonstrateWebClient(txt_fileroad.Text, credentials);
}public void DemonstrateWebClient(string sourcestr, NetworkCredential credentials)
{
WebClient webClient = new WebClient();
webClient.Credentials = credentials; // Upload file.
try
{
string fileName = sourcestr.Substring(sourcestr.LastIndexOf("\\") + 1, sourcestr.Length - sourcestr.LastIndexOf("\\") - 1); //ReadFileName("upload");
//string uploadAddress = serverAddress + fileName;
Uri address1 = new Uri("http://www.toploria.com.cn/TKservice/Pictures/");
//Console.WriteLine("Uploading file " + uploadAddress);
webClient.UploadFile(address1, fileName);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
} webClient.Dispose();
}
解决方案 »
- gridview控件的deleting事件怎么写
- 组合算法
- 线程的异常处理
- 关于C#GDI画图闪屏问题
- 小弟遇到一个问题,在使用Datagridview控件的时候,由于与数据库连接部分的代码全部封装在一个类中,主页代码无法取得sqldataAdapter对象,不好用不用sqldataAdapter的UPdata()跟新,我要如何实现数据的跟新啊?小弟第一次
- winform小问题求助!散分30!
- 哪位有这样的socket通讯源代码,急急,谢谢!
- 利用HyperLink控件能够将服务器上的文件下载吗
- 定义的 handler 打断点 为什么不执行呢
- 请问有关System.Net.Sockets的问题:LingerOption有什么作用?
- 如何向word模版的域中插入值?
- .net windows forms窗体开发方式问题
protected void Button1_Click(object sender, EventArgs e)
{
string savePath = "c:\\";
string fileName = FileUpload1.FileName;
if (FileUpload1.HasFile)//FileUpload1是控件ID {
string pathToCheck = savePath + fileName;
if (!System.IO.File.Exists(pathToCheck))
{
FileUpload1.PostedFile.SaveAs(pathToCheck);
}
else
{
int count = 2;
string pathToCheck2 = savePath + count.ToString() + fileName;
FileUpload1.PostedFile.SaveAs(pathToCheck2);
}
}
else
{
this.Button1.Text = "No File";
}
}
希望能对楼主有用,也能够起到抛砖引玉的作用,谢谢.