远程建立文件夹,再上传文件有很多种方式,比如权限够的话,你可以调用远程计算机cmd命令去创建文件夹,也可以用ftp协议去创建文件夹,当然最简单的就是利用webservice了
服务器必须先建立创建文件方法的webservice
public class CreateDir : System.Web.Services.WebService
{
[WebMethod]
public int CreateDirectory(string bnum, string cnum, string snum)
{
//string path = @"E:/Learn/Test/Test/img";
string path = @"e:/ImageTest";
string bnumDire = path + "/" + bnum;
if (Directory.Exists(bnumDire) == false)
{
Directory.CreateDirectory(bnumDire);
}
string cnumDire = bnumDire + "/" + cnum;
if (Directory.Exists(cnumDire) == false)
{
Directory.CreateDirectory(cnumDire);
}
string snumDire = cnumDire = cnumDire + "/" + snum;
if (Directory.Exists(snumDire) == false)
{
Directory.CreateDirectory(snumDire);
}
return 1;
}
[WebMethod]
public int GreateDirectory(string list)
{
string path=@"e:/ImageTest";
path=path+"/"+list;
if (Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return 1;
}
}
然后在你的winform程序里引用发布的webservice,最后去传参等去调用执行这个方法,最后用webclient,FileStream去上传文件
urlString = urlString +"01/001/0001/" + fileNewName;
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
// 要上传的文件
FileStream fs = new FileStream(fullName, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
try
{
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = client.OpenWrite(urlString, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
else
{
System.Windows.Forms.MessageBox.Show("上传失败!");
}
postStream.Close();
System.Windows.Forms.MessageBox.Show("上传成功!");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{
fs.Close();
r.Close();
client.Dispose();
}
完全可以实现创建文件夹然后再上传文件的效果