2014-09-25 90 views
2

我有来自Android的上传图像文件在ASP净C#远程web服务的问题。上传图像回空文件

的问题是,从Android电子发送到远程服务器的图像文件是零千字节;上传工作,在远程服务器上正确的文件夹中上传的图像文件名是正确的,我没有在调试Android和调试asp网络错误,但这个图像是空的。

我将不胜感激您的帮助。

在此先感谢

的Android代码的Java类:

File mFile = new File("/storage/emulated/0/image_file.jpg"); 
    if(mFile.exists()){ 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     Bitmap bmp = BitmapFactory.decodeFile(mFile.getAbsolutePath()); 
     bmp.recycle(); 
     bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byte[] byteArray = stream.toByteArray(); 
     Request.addProperty("byteArray", byteArray); 
    } 

在ASP.NET的C#的WebService

[WebMethod] 
public string sendUpload(Byte[] byteArray, string fileName) 
{ 
    string Path; 
    Path = "D:\\Inetpub\\wwwroot\\myAppAndroid\\uploads\\" + fileName; 
    FileStream objfilestream = new FileStream(Path, FileMode.Create, 
               FileAccess.ReadWrite); 
    objfilestream.Close();  

    return byteArray + "; " + fileName.ToString(); 
} 

首先编辑的问题,我有错误在这行

file.Write(byteArray, 0, byteArray.Length); 

[WebMethod] 
public string sendUpload(Byte[] byteArray, string fileName) 
{ 
    string strdocPath; 
    strdocPath = "D:\\Inetpub\\wwwroot\\myAppAndroid\\uploads\\" + fileName; 

    System.IO.FileStream file = System.IO.File.Create(HttpContext.Current.Server.MapPath(".\\myAppAndroid\\uploads\\" + fileName)); 

    file.Write(byteArray, 0, byteArray.Length); 
    file.Close(); 

    return byteArray + "; " + fileName.ToString(); 
} 

回答

0

你sh乌尔德写接收到的文件的文件系统上的内容:

[WebMethod] 
    public string sendUpload(Byte[] byteArray, string fileName) 
    { 
     string Path; 
     Path = "D:\\Inetpub\\wwwroot\\myAppAndroid\\uploads\\" + fileName; 
     FileStream objfilestream = new FileStream(Path, FileMode.Create, 
                FileAccess.ReadWrite); 
// here some code is missing 
     objfilestream.Close();  

     return byteArray + "; " + fileName.ToString(); 
    } 

你有Byte[]作为输入,你从来没有在服务器磁盘上写。

+0

如果他从来没有使用过,他会发现他自己的错误,因为VS发生标记未使用的变量,并给予警告。但他用它,只是不写它的文件系统,但它返回到用户(@ChevyMarkSunderland:你为什么要像一个MEG发回给用户时,他只是把它发送给你 - 他会一直保持一个副本,不需要回...;)) – Alexander 2014-09-25 08:40:23

+0

谢谢你,但如果问题是从Android或web服务... – 2014-09-25 08:45:03

+0

造成@ChevyMarkSunderland您的Web服务的C#代码不是写文件的内容,我不明白在文件系统上。如果您不在文件系统上写入内容,则文件将始终为空。你必须写东西到该文件中没有空文件... – Paolo 2014-09-25 08:48:33