我在这里有以下代码:
string imgPath = GetPathToImage(uri);
int imageHeight = 250;
int imageWidth = 200;
var bitmap = imgPath.LoadAndResizeBitmap2(imageWidth, imageHeight);
UploadBitmapAsync(bitmap);
async void UploadBitmapAsync(Bitmap bitmap)
{
const string UPLOAD_IMAGE = "http://" + HostAddress.Main + "/fsrservice/atxm";
byte[] bitmapData;
var stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 0, stream);
bitmapData = stream.ToArray();
var fileContent = new ByteArrayContent(bitmapData);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "file",
FileName = "my_uploaded_image.jpg"
};
string boundary = "---8d0f01e6b3b5dafaaadaad";
MultipartFormDataContent multipartContent = new MultipartFormDataContent(boundary);
multipartContent.Add(fileContent);
HttpClientHandler clientHandler = new HttpClientHandler();
HttpClient httpClient = new HttpClient(clientHandler);
HttpResponseMessage response = await httpClient.PostAsync(UPLOAD_IMAGE, multipartContent);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine("SUCCESS! " + content);
bitmap.Recycle();
}
else
{
Console.WriteLine("NOT SUCCESSFUL!");
}
}
它不会给出任何错误(给出NOT SUCCESSFUL
),但图像不会上传到服务器。我正在使用Xamarin Android,我的代码基于Upload Bitmap Image Using HTTP Multipart in Xamarin Android。 fsrservice
是我用于我的应用的网络服务,这是我将文件夹atxm
放在我保存文件的位置。可能是什么问题?
修改
好的,所以我仍然试图解决这个问题,然后我偶然发现了这种方法,WebClient.UploadData Method。我将此添加到我的Web服务并创建了一个.aspx,但我看不到上传的文件到该文件夹。从客户端,文件通过字节数组传递。
以下是我的代码:
Service1.svc.cs
public string FileUpload(EmployeeDetails userInfo)
{
Database context = new Database();
List<byte[]> byteArray = JsonConvert.DeserializeObject<List<byte[]>>(userInfo.File);
List<string> fileName = JsonConvert.DeserializeObject<List<string>>(userInfo.FileName);
List<string> editedName = new List<string>();
Attachment obj = new Attachment();
for (int i = 1; i <= byteArray.Count; i++)
{
for (int j = 0; j < fileName.Count; j++)
{
// SOME CODE HERE
}
}
context.SubmitChanges();
for (int i = 0; i < byteArray.Count; i++)
{
byte[] b = byteArray[i];
string uriString = "http://" + HostAddress.Address + "/FSRService/upload.aspx";
WebClient myWebClient = new WebClient();
myWebClient.UploadData(uriString, "POST", b);
}
string msg = null;
return msg;
}
upload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="upload.aspx.cs" Inherits="WcfService.upload" %>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>
<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
foreach(string f in Request.Files.AllKeys) {
HttpPostedFile file = Request.Files[f];
file.SaveAs("c:\\inetpub\\wwwroot\\FSRService\\ATXM\\" + file.FileName);
}
}
</Script>
<html>
<body>
<p> Upload complete. </p>
</body>
</html>
问题:
答案 0 :(得分:1)
好 我喜欢的是这样的。
首先,我使用一个对象作为文件:
class FileTransfer
{
public byte[] content;
public string name;
public FileTransfer(string name, byte[] content)
{
this.name= name;
this.content= content;
}
}
然后我在Asp.Net上有一个web服务,看起来像是:
[WebMethod]
public string GetFile(string data)//the file comes serialized to Json
{
//Transforms the string into the object
FileTransfer recived = new JavaScriptSerializer().Deserialize<FileTransfer>(data);
string path = "c:\\inetpub\\wwwroot\\FSRService\\ATXM\\"+recived.name; //you could also do this in the object builder
File.WriteAllBytes(path, recived.content);
}
现在在Xamarin中,您可以通过右键单击项目,添加,添加Web引用直接添加.asmx webservice作为webreference,它将创建一个&#34;类&#34;使用异步函数。
你可以像这样使用它:
public void SendFile(File fileToSend)
{
//process the file
FileStream objfilestream = new FileStream(fileToSend.FullName, FileMode.Open, FileAccess.Read);
int len = (int)objfilestream.Length;
Byte[] documentcontents = new Byte[len];
objfilestream.Read(documentcontents, 0, len);
objfilestream.Close();
FileTransfer newFiletoSend = new FileTransfer(file.Name, documentcontents);
string raw = new JavaScriptSerializer().Serialize(newFiletoSend);
//send it
nameOfTheWebServiceClass.nameOfTheAsmxFile service = new nameOfTheWebServiceClass.nameOfTheAsmxFile();
service.GetFile(raw); //GetFile is the name of the funcion you want to use in the .asmx webservice.
}