文件路径已知,怎么传?
回答简单点,用哪个类,调哪几个函数OK了
以前没用过,查MSDN,上面有FileUpload,可它要选文件,不然没法直接与string类型的串关联
还有一个HttpPostedFile好像也必须要用request.files,也不能直接与string关联
回答简单点,用哪个类,调哪几个函数OK了
以前没用过,查MSDN,上面有FileUpload,可它要选文件,不然没法直接与string类型的串关联
还有一个HttpPostedFile好像也必须要用request.files,也不能直接与string关联
{
//遍历File表单元素
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
//状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息分别为:<hr color=red>");
int fileCount;
int filecount = files.Count;
try
{
for(fileCount = 0;fileCount<files.Count;fileCount++)
{
//定义访问客户端上传文件的对象
System.Web.HttpPostedFile postedFile = files[fileCount];
string fileName, fileExtension;
//取得上传得文件名
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if(fileName != String.Empty)
{
//取得文件的扩展名
fileExtension = System.IO.Path.GetExtension(fileName);
//上传的文件信息
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr color=red>");
//保存到指定的文件夹
postedFile.SaveAs(Server.MapPath("upedFile/") + fileName);
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch(System.Exception error)
{
strStatus.Text = error.Message;
return false;
}
}
如果要上传还需要调用网络相关的类
步骤就是打开本地文件(System.IO.FileInfo),
传输到服务器(http,ftp),
保存文件(System.IO.FileInfo)最好是用ftp
晕,我说了,我没有表单,我的文件路径是存在一个数组里面的。全部已知,现在只需要将文件传到服务器上,难到要用SOCKET???那岂不成了做WINDOWS应用程序了
//啊?不通过web页面,那你的路径数组怎么得到?
/// 使用WebClient上传文件测试
/// </summary>
public class WebClientTest
{
public static void Main(string[] args)
{
// Server URL
string uriString = "http://localhost/FileUpLoad/2006327143303_Grid1.jpg";
// Local Directory File Info
string fileName = @"c:\temp\2006327143303_Grid1.jpg"; // Create a new WebClient instance.
WebClient myWebClient = new WebClient(); FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read); BinaryReader br = new BinaryReader(fs); Byte[] postArray = br.ReadBytes(Convert.ToInt32(fs.Length)); Stream postStream = myWebClient.OpenWrite(uriString,"PUT"); if(postStream.CanWrite)
{
postStream.Write(postArray,0,postArray.Length);
}
postStream.Close();
fs.Close();
}
}
====>
楼主用的asp.net吗? 这里的文本文档是从服务器端读过来的吗? 如果是从服务器端读出来的, 那这些路径对于不同的客户端都有效吗?
<asp:Button ID="ButtonUpload" runat="server" OnClick="ButtonUpload_Click" Text="Button" />
protected void ButtonUpload_Click(object sender, EventArgs e)
{
this.fileUpload.SaveAs("你想保存到你的服务器位置");
}