我在将图像文件从 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/