我想将文件从Silverlight发布到服务器。
目前在Silverlight中我有FileInfo myFile;
。在服务器端,我需要将此文件放在Request.Files
集合中。
我的问题是如何将myFile从Silverlight发布到服务器,以便能够在服务器端的Request.Files集合中访问它?
谢谢。
答案 0 :(得分:0)
这是我用来将文件流发布到服务器的代码。
private void LayoutRoot_Drop(object sender, DragEventArgs e)
{
_dropEventOccured = "Yes";
FileInfo[] files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
MessageBox.Show(files.Length.ToString());
//MessageBox.Show("Dropped");
foreach (FileInfo fi in files)
{
_fileRead = "Yes";
_file = fi;
FileStream fs = _file.OpenRead();
objByte = ReadFully(fs);
fs.Close();
Post();
}
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
private void Post()
{
HttpWebRequest request = null;
//request = HttpWebRequest.Create("http://localhost/ajax.aspx") as HttpWebRequest;
//request = HttpWebRequest.Create("http://localhost/ajax.aspx") as HttpWebRequest;
request = HttpWebRequest.Create(string.Format("http://localhost/ajax.aspx?name={0}",_file.Name )) as HttpWebRequest;
//request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.ContentType = "multipart/form-data";
request.Method = "POST"; // Doesn't work with "GET"
request.ContentLength = objByte.Length;
if (request.Method == "GET")
{
request.BeginGetResponse(this.GetRequestStreamCallback, request);
}
else
{
MessageBox.Show("Posting...");
request.BeginGetRequestStream(this.GetRequestStreamCallback, request);
}
}
private void GetRequestStreamCallback(IAsyncResult result)
{
System.Threading.Thread.Sleep(1000);
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
using (Stream stream = request.EndGetRequestStream(result))
{
stream.Write(objByte, 0, objByte.Length);
stream.Close();
}
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
//HttpWebResponse response = (HttpWebResponse)result.EndGetResponse(asyncResult);
}
private void GetResponseCallback(IAsyncResult result)
{
try
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
while (!request.HaveResponse)
{
System.Threading.Thread.Sleep(1000);
}
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
Console.WriteLine(response.StatusDescription);
}
catch (Exception ex)
{
}
}
这是服务器端的代码:
protected void Page_Load(object sender, EventArgs e)
{
string saveTo = Server.MapPath("~/App_Data/") + Request.QueryString["name"];
// create a write stream
FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.Write);
// write to the stream
ReadWriteStream(Request.InputStream, writeStream);
}
private void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}