一个关于文件上传下载到文件到服务器的代码
msdn上有这方面的code。
在手机上或许也可以借鉴。
文件服务器的为http文件结构时,本地控制远程的服务器上的文件:
public void UploadFileBinary(string localFile, string uploadUrl)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
//req.Credentials = new NetworkCredential("Administrator", "aaa");//用户名,密码
req.PreAuthenticate = true;
req.Method = "PUT";
req.AllowWriteStreamBuffering = true;
// Retrieve request stream
Stream reqStream = req.GetRequestStream();
// Open the local file
FileStream rdr = new FileStream(localFile, FileMode.Open);
// Allocate byte buffer to hold file contents
byte[] inData = new byte[4096];
// loop through the local file reading each data block
// and writing to the request stream buffer
int bytesRead = rdr.Read(inData, 0, inData.Length);
while (bytesRead > 0)
{
reqStream.Write(inData, 0, bytesRead);
bytesRead = rdr.Read(inData, 0, inData.Length);
}
rdr.Close();
reqStream.Close();
req.GetResponse();
}
public void DownloadFileBinary(string localFile, string downloadUrl)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl);
req.Method = "GET";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
// Retrieve response stream
Stream respStream = resp.GetResponseStream();
// Create local file
FileStream wrtr = new FileStream(localFile, FileMode.Create);
// Allocate byte buffer to hold stream contents
byte[] inData = new byte[4096];
// loop through response stream reading each data block
// and writing to the local file
int bytesRead = respStream.Read(inData, 0, inData.Length);
while (bytesRead > 0)
{
wrtr.Write(inData, 0, bytesRead);
bytesRead = respStream.Read(inData, 0, inData.Length);
}
respStream.Close();
wrtr.Close();
}
文件存在的判断并没有写,不过也可以用强制转换HttpWebRequest来获取。