继承人使用WCF其余文件上传的例子:
[ServiceContract]
interface IUploader
{
[WebInvoke(UriTemplate = "FileUpload?public_id={public_id}&tags={tags}",
Method = "POST",
ResponseFormat = WebMessageFormat.Json)]
UploadImageResult UploadImage(Stream fileContents, string public_id, string tags);
}
public class Uploader : IUploader
{
public UploadImageResult UploadImage(Stream fileContents, string public_id, string tags)
{
try
{
byte[] buffer = new byte[32768];
FileStream fs = File.Create(Path.Combine(rootFolderPath, @"test\test.jpg"));
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = fileContents.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
fs.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
fs.Close();
}
catch(Exception ex)
{
...
}
}
}
要调用使用C#
// Create the REST URL.
string requestUrl = @"http://localhost:2949/fileupload?public_id=id&tags=tags";
//file to upload make sure it exist
string filename = @"C:\temp\ImageUploaderService\vega.jpg";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.Method = "POST";
//request.ContentType = "text/plain";
request.ContentType = "application/json; charset=utf-8";
byte[] fileToSend = File.ReadAllBytes(filename);
request.ContentLength = fileToSend.Length;
using (Stream requestStream = request.GetRequestStream())
{
// Send the file as body request.
requestStream.Write(fileToSend, 0, fileToSend.Length);
requestStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine("HTTP/{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription);
string text;
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
dynamic testObj = Newtonsoft.Json.JsonConvert.DeserializeObject(text);
Console.WriteLine(string.Format("response url is {0}", testObj.url));
}
问题#2中,可以只是追加使用该请求的URL中的键/值对中的数据等的例子。
希望这会有所帮助。
是否要将图像从Android上传到FTP服务器? – 2013-05-12 16:38:58
我想上传图像到我的HTTP服务器不是FTP,使用我在问题中链接的代码 – 2013-05-13 08:27:47
你不应该把任何东西放在图像bytearray上,也许在while循环后你可以发送另一个字符串,这意味着**发送是OK * * – 2013-05-13 08:49:18