c# - 上传图片返回空文件

标签 c# android asp.net

我在将图像文件从 Android 上传到 asp net c# 中的远程 Web 服务时遇到问题。

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

非常感谢您的帮助。

提前致谢

java类中的android代码:

    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();
}

最佳答案

你应该在文件系统上写入接收到的文件的内容:

    [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[] 作为输入,你永远不会写在服务器磁盘上。

关于c# - 上传图片返回空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26033981/

相关文章:

c# - 将 Excel 互操作的 Range.Value2 转换为字符串

c# - "Parameter is not valid. At System.Drawing.Bitmap..Ctor (Stream stream)."

c# - 在完全初始化结构之前访问结构的字段

java - 为同一个共享库调用 System.loadLibrary 两次

android - 从图库中读取位图并将其写入 SD 卡失败

C# 上传文件到服务器 - 客户端和服务器端

asp.net - SSO 的实现有哪些潜在的安全问题?

c# - asp.net 如何将值从服务器端页面传递到客户端函数

c# - 访问不在绑定(bind)列表中的 Datagridview 列

带叠加层的 Android 应用介绍