首页 新闻 赞助 找找看

各位有谁做过,从客户端传来图片文件,c#这边应该怎么写代码接收?

0
[已解决问题] 解决于 2014-03-10 09:30

 就是我这边通过写接口,然后,客户端那边调用我写的接口,然后,传给我图片文件,

我的问题就是,不知道怎么去写这个接口处理从客户端传来的图片文件这东东,

 

 

 

我是打算建一个图片文件夹,专门来存储这些文件,数据库里面只是存储图片路径和图片名称等

 

 

接收:

image = "iVBORw0KGgoAAAAEQ2dCSVAAIAYsuHdmAAAADUlIRFIAAAFAAAAB4AgGAAAA1Iy0RAAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAAcaURPVAAAAAIAAAAAAAAA8AAAACgAAADwAAAA8AABXhed3r5jAAFd40lEQVTE/eebVFXCPX774hnprurKqatzjuSczYloAhRUJEcRRFSUbJioTnAcc1YMiJJzzjlD09DdpA5A5zrnMHPP3LOeF3ufc..."

c#服务端接收:

var mapPath = Server.MapPath("~/FucUpload/");
var newName = mapPath + System.DateTime.Now.ToFileTime();

byte[] bytes = Convert.FromBase64String(image);
MemoryStream memStream = new MemoryStream(bytes);
BinaryFormatter binFormatter = new BinaryFormatter();
Image img = (Image)binFormatter.Deserialize(memStream);

img.Save(newName, System.Drawing.Imaging.ImageFormat.Jpeg);

问题补充:

安卓客户端用的第三方控件的地址是:https://github.com/AFNetworking/AFNetworking

有谁懂的请帮我看看,我会提高悬赏的,拜托了。。。跪拜各位大神了。。。。

客户端用的是这个方法来调用我接口的,我不懂,怎么写接口,安卓客户端

 错误代码是:

2014-03-04 17:20:22.446 ImageUpload[9773:60b] Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)" UserInfo=0x15664ae0 {NSErrorFailingURLKey=http://xx/open/pic/UploadCameraFun1, NSLocalizedDescription=Request failed: internal server error (500), NSUnderlyingError=0x15684a10 "Request failed: unacceptable content-type: text/html", AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x1557acf0> { URL: http://xx/open/pic/UploadCameraFun1 } { status code: 500, headers { 

"Cache-Control" = private; 

Connection = "keep-alive"; 

"Content-Length" = 6192; 

"Content-Type" = "text/html; charset=utf-8"; 

Date = "Tue, 04 Mar 2014 09:20:19 GMT"; 

Server = "nginx/1.4.1"; 

"X-AspNet-Version" = "4.0.30319"; 

"X-Powered-By" = "ASP.NET"; 

} }}

c#这边写的代码是

 public JsonResult UploadCameraFun(byte[] files,string remark,string mood)
        {
            bool flag = false;
            #region 上传图片
            var newName = System.DateTime.Now.ToFileTime() + ".jpg";
            var mapPath = Server.MapPath("~/FucUpload/");
            var sourcePhysicalPath = Path.Combine(Server.MapPath(mapPath), newName);
            FileStream fs = new FileStream(sourcePhysicalPath, FileMode.Append, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(files, 0, files.Length);
            bw.Close();
            fs.Close();

View Code

福气满满好运连连的主页 福气满满好运连连 | 初学一级 | 园豆:60
提问于:2014-02-24 11:01
< >
分享
最佳答案
0

呵呵,用的base64,真是悲剧的问题

福气满满好运连连 | 初学一级 |园豆:60 | 2014-03-05 13:56
其他回答(12)
0

是客户端通过网页上传吗?

Launcher | 园豆:45045 (高人七级) | 2014-02-24 11:04

c#这端,通过写接口,然后,客户端,那边调用,传图片文件为我

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 11:05

@魔女小溪: 是进程内调用么?

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2014-02-24 11:35

@Launcher: 不是

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 17:11

@魔女小溪: 你一贴图片,我就知道了,客户端通过 HTTP POST 方法上传数据。你是用 Asp.Net 来建立 Web 服务器吗?

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2014-02-24 17:14

@Launcher: 我用的是mvc,自己建立了一个controller,然后写了一个方法,供安卓工程师调用,现在就是,我不知道怎么写这个方法

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 17:15

@魔女小溪: 我是菜鸟呀。。。呜呜,我都没用接触过这东东,所以,很茫然

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 17:16

@Launcher: 这里面用的是js去调用,我的是,安卓客户端那边调用接口,然后传数据过来,我就是不知道怎么定义参数

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 17:24

@Launcher: 我就是不懂安卓客户端,传来的数据是什么格式,在c#中,用什么样类型的参数去接收,我上面贴出了图片,但是,我看不懂

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 17:29

@魔女小溪: 抓个包不就看到请求的内容了吗?

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2014-02-24 17:33

@Launcher: 我可以说句让你磨牙的话吗?我都不懂怎么抓包,呜呜呜,苍天呐。。。。

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 17:37

@魔女小溪: 你按照:http://stackoverflow.com/questions/5149116/mvc-how-to-post-file-upload-and-other-form-fields-to-one-action

来写你的 Controller,然后让他们按照这个格式来提交请求。

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2014-02-24 17:42

@魔女小溪: 巧啊

支持(0) 反对(0) 祁临芯 | 园豆:251 (菜鸟二级) | 2014-02-25 17:21
0

客户端是如何传送?

亲哥、嘿 | 园豆:183 (初学一级) | 2014-02-24 11:04

c#这端,通过写接口,然后,客户端,那边调用,传图片文件为我

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 11:05

@魔女小溪: 那你的接口是怎样写的呢,是直接存到某个路径还是将流存在内存对象?

支持(0) 反对(0) 亲哥、嘿 | 园豆:183 (初学一级) | 2014-02-24 11:08

@魔女小溪: 或者说你的接口还没有实现?

支持(0) 反对(0) 亲哥、嘿 | 园豆:183 (初学一级) | 2014-02-24 11:08

@亲哥、嘿: 我是打算建一个图片文件夹,专门来存储这些文件,数据库里面只是存储图片路径和图片名称等

支持(1) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 11:09

@亲哥、嘿: 我的接口还没开始写,我就是不知道怎么写接口接收从客户端传来的图片文件

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 11:10

@魔女小溪: 在你的接口里给一个"byte[]"这样的参,在实现里将这个参数写到你的文件夹,将路径和名字存到你的数据库,是否就可以了呢。

支持(0) 反对(0) 亲哥、嘿 | 园豆:183 (初学一级) | 2014-02-24 11:14

@亲哥、嘿: 我有点不懂的就是,客户端那边调用我这个接口的话,我接口方法参数若写成byte[]这个类型,那么客户端调用的话,会自动转换成这个byte[]类型是吗?

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 11:17

@魔女小溪: 如果想传图片的话,我都是将图片从本地读到程序中,那肯定是流,所以你给一个byte[]是最省事的办法

支持(0) 反对(0) 亲哥、嘿 | 园豆:183 (初学一级) | 2014-02-24 11:20

@亲哥、嘿: 本地我知道怎么上传

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 11:21

@魔女小溪:  或许你可以这样定义你的接口

void SendImage(string ImgName,byte[] ImgData);

支持(0) 反对(0) 亲哥、嘿 | 园豆:183 (初学一级) | 2014-02-24 11:22

@亲哥、嘿: 你可以写详细一点吗?怎么操作这个 byte[]这东西,我不知道怎么弄,呵呵

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 11:24

@魔女小溪: 好吧可能我没说清楚,我的意思是如果客户端想得到一张图片,那么他肯定是得到的文件流,那么在调用你的接口时,就很方便将他得到的数据给你传过来了

支持(0) 反对(0) 亲哥、嘿 | 园豆:183 (初学一级) | 2014-02-24 11:25

@亲哥、嘿: 恩恩,这个我懂,我的意思是,在这个接口的实现方法中,我怎么去处理这个byte[],因为我没有处理过,就是怎么上传

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 11:28

@魔女小溪: System.IO.File.WriteAllBytes(@"你的路径,要加名字和.jpg神马的", ImageData);

诶~ 弱弱的说一句,我还没用过这个写文件

不过你去了解下IO就知道啦,啊哈~

支持(0) 反对(0) 亲哥、嘿 | 园豆:183 (初学一级) | 2014-02-24 11:30

@亲哥、嘿: 恩,我自己去网上找找,呵呵,谢谢

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-24 11:30
1

文件上传有两个基本参数:源路径和目标路径,客户端获取从服务端返回的目标路径

iEvent | 园豆:529 (小虾三级) | 2014-02-24 11:06
1

客户端把图片用切分 然后用TCP通信传递到指定服务端,服务端接收数据 保存文件

大芝麻 | 园豆:4 (初学一级) | 2014-02-24 13:11
1

既然是cs通讯,有两种选择:

1.你可以把图片存入数据库的某个字段中,而不存是文件名,直接存比特流(byte[])。

2.将图片存入文件系统,然后把路径写入数据库中。

接收和发送,你直接发送比特流就可以了呀!二进制传输,针对任何类型的文件都是一样的。

空明流光 | 园豆:106 (初学一级) | 2014-02-24 14:15
1

tcp传文件字节数组就好了

滴答的雨 | 园豆:3681 (老鸟四级) | 2014-02-24 15:27
1

建议看看这个http://www.tracefact.net/CSharp-Programming/Network-Programming-Part5.aspx

阿磊ing | 园豆:2 (初学一级) | 2014-02-24 17:34
1
[HttpPost]
        public ActionResult Index(HttpPostedFileBase pic)
        {
            string fileFormat = Path.GetExtension(pic.FileName);
            string fileName = Guid.NewGuid().ToString("N");
            string imageSaveDirectory = Server.MapPath("~/Upload");

            if (!Directory.Exists(imageSaveDirectory))
            {
                Directory.CreateDirectory(imageSaveDirectory);
            }

            bool result = false;
            string imageSavePath = Path.Combine(imageSaveDirectory, fileName + fileFormat);
            pic.SaveAs(imageSavePath);
            if (System.IO.File.Exists(imageSavePath))
            {
                result = true;
            }

            //上传成功
            if (result)
            {
                return Json(new { result = "1", msg = "OK" });
            }
            else
            {
                //上传失败
                return Json(new { result = "0", msg = "Fail"});
            }
        }
pstune | 园豆:239 (菜鸟二级) | 2014-02-24 19:29
0
我项目中的一些代码,也许有帮助。

[HttpPost]
public ActionResult LEdit(Shop_MemberLevel _shop_MemberLevel, HttpPostedFileBase ImgPath) { _shopMemeber.UpdateLevel(_shop_MemberLevel, ImgPath); return SuccessMsg("AdminMemberMemberLevel"); } public void UpdateLevel(Shop_MemberLevel shopMemeberLevel, HttpPostedFileBase logoFile) { shopMemeberLevel.ImgPath = SaveImages(logoFile, new int[][] { new int[] { 100, 30 }, new int[] { 200, 10 }, }, "W", "Download\\Level"); _unitOfWork.Shop_MemberLevelRepository().Update(shopMemeberLevel); _unitOfWork.Save(); } /// <summary> /// 保存图片以及生产缩略图 /// </summary> /// <param name="imageFileBase">图片流</param> /// <param name="imageName">保存的图片名</param> /// <param name="sizes">缩略图大小,sizes为形如[ [width1,height1], [width2,height2] ]的数组</param> /// <param name="mode">生成缩略的模式,HW-拉伸,W-指定宽度,H-指定高度,Cut-剪裁</param> /// <param name="directory">目录,前后无斜杠</param> /// <returns>保存的文件夹路径</returns> public string SaveImages(HttpPostedFileBase imageFileBase, int[][] sizes, string mode, string directory = "") { //检查并创建目录 string path = HttpContext.Current.Server.MapPath("/"); string virtulpath = ""; if (!string.IsNullOrEmpty(directory)) { virtulpath += directory + "\\"; } path += virtulpath; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //创建GUID var guid = Guid.NewGuid(); //保存原图 imageFileBase.SaveAs(path + guid + ".jpg"); /*生成缩略图 * sizes为形如 * [ [width, height],[width2, height2]] *的二维数组 */ foreach (int[] size in sizes) { if (size.Count() != 2) { continue; } int width = size[0]; int height = size[1]; string thumPath = path + guid + "_" + width + "x" + height + ".jpg"; MakeThumNail(path + guid + ".jpg", thumPath, width, height, mode); } return "\\" + virtulpath + guid + ".jpg"; } /// <summary> /// 生成缩略图 /// </summary> /// <param name="orginalImagePat">原图片地址</param> /// <param name="thumNailPath">缩略图地址</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="model">生成缩略的模式,HW-拉伸,W-指定宽度,H-指定高度,Cut-剪裁</param> public void MakeThumNail(string originalImagePath, string thumNailPath, int width, int height, string model) { //originalImagePath = MapPath(originalImagePath); //thumNailPath = HttpContext.Current.Server.MapPath(thumNailPath); System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int thumWidth = width; //缩略图的宽度 int thumHeight = height; //缩略图的高度 int x = 0; int y = 0; int originalWidth = originalImage.Width; //原始图片的宽度 int originalHeight = originalImage.Height; //原始图片的高度 switch (model) { case "HW": //指定高宽缩放,可能变形 break; case "W": //指定宽度,高度按照比例缩放 thumHeight = originalImage.Height * width / originalImage.Width; break; case "H": //指定高度,宽度按照等比例缩放 thumWidth = originalImage.Width * height / originalImage.Height; break; case "Cut": if ((double)originalImage.Width / (double)originalImage.Height > (double)thumWidth / (double)thumHeight) { originalHeight = originalImage.Height; originalWidth = originalImage.Height * thumWidth / thumHeight; y = 0; x = (originalImage.Width - originalWidth) / 2; } else { originalWidth = originalImage.Width; originalHeight = originalWidth * height / thumWidth; x = 0; y = (originalImage.Height - originalHeight) / 2; } break; default: break; } //新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(thumWidth, thumHeight); //新建一个画板 System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap); //设置高质量查值法 graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 graphic.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 graphic.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, thumWidth, thumHeight), new System.Drawing.Rectangle(x, y, originalWidth, originalHeight), System.Drawing.GraphicsUnit.Pixel); try { bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { throw ex; } finally { originalImage.Dispose(); bitmap.Dispose(); graphic.Dispose(); } }
祁临芯 | 园豆:251 (菜鸟二级) | 2014-02-25 16:55

SaveImages你项目中的这个方法,是用来接收客户端,也就是手机客户端那边传来的图片数据吗?他那边是怎么传图片的,有没有安卓那边传图片的方法呀?谢谢

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2014-02-28 10:42
0

就是在那的Request.Files里面

丫的 | 园豆:1575 (小虾三级) | 2014-03-04 15:54
0

对头,Request.Files;

 HttpFileCollection uploadedFiles = Request.Files;
        for (int i = 0; i < uploadedFiles.Count; i++)
        {
            HttpPostedFile F = uploadedFiles[i];
            if (uploadedFiles[i] != null && F.ContentLength > 0)
            {
                //得到上传文件的长度
                int upFileLength = F.ContentLength;
                //Loger.Debug("upFileLength==>" + upFileLength);
                //得到上传文件的客户端MIME类型
                string contentType = F.ContentType;
                byte[] FileArray = new Byte[upFileLength];
                Stream fileStream = F.InputStream;
                fileStream.Read(FileArray, 0, upFileLength);
                string newName = F.FileName.Substring(F.FileName.LastIndexOf("\\") + 1);
                string id = newName.Substring(0, newName.LastIndexOf("."));
                string pathUrl = "c:/fileroot/" + newName; //服务器本地路径
                F.SaveAs(pathUrl);
            }
        }

大概就是这样子....

cs_clinging | 园豆:209 (菜鸟二级) | 2014-03-04 17:09
0

你好,安卓那边传图,服务器接受。可以用base64 去做。我想问下楼主直接用流 成功了吗。可以交流下qq454451808

看,有灰机在飞 | 园豆:138 (初学一级) | 2015-03-02 15:45

我用的就是base64,

支持(0) 反对(0) 福气满满好运连连 | 园豆:60 (初学一级) | 2015-03-06 16:40

@魔女小溪: 噢,请问楼主做过安卓多图上传的服务器

支持(0) 反对(0) 看,有灰机在飞 | 园豆:138 (初学一级) | 2015-03-06 17:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册