将图像从Android上传到asp.net

时间:2012-06-19 09:10:44

标签: android wcf file-upload

我收到以下错误:

Cannot process the message because the content type
'multipart/form-data;
boundary=0JxmnFLOmjvutAKrtR1bmPCqpG8M6WbwE-aAvh8' was not the expected
type 'text/xml; charset=utf-8'

Android代码:

        HttpPost httppost = new HttpPost("http://192.168.9.103/AndroidService/MediaUploadService.svc/uploadFile1");
        File photo = new File(Environment.getExternalStorageDirectory(), "01.jpg");
        MultipartEntity t = new MultipartEntity();
        t.addPart("t", new FileBody(photo));
        httppost.setEntity(t);
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httppost);

WCF代码:

public class MediaUploadService : IMediaUploadService
{
    public void UploadFile(Stream fileContents)
    {
        byte[] buffer = new byte[10000];
        int bytesRead, totalBytesRead = 0;
        do
        {
            bytesRead = fileContents.Read(buffer, 0, buffer.Length);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);
        File.WriteAllText(@"D:\\Vechile\log2.txt", totalBytesRead.ToString());
    }
}

有人能帮我做这个过程吗?

我的问题: -
需要将图像文件从Android上传到服务器(通过IIS - > .asp,.asmx,.svc等)

有没有人建议我解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以做的是将2个参数发送到网址:

  • fileName =文件名
  • fileContent =以base64编码的文件内容

在你的asp文件中,只需获取2个参数内容,进行base 64解码,然后将其写入名为filename参数的文件中......