【发布时间】:2015-01-05 10:04:06
【问题描述】:
我写了一个ios客户端发送*.jpg到服务器,服务器使用WCF。现在我可以从客户端获取流,但问题是我不知道从哪里获取文件名和 mimeType。另一个问题是我发现传递给服务器的流比客户端的大,当我测试时,我发现服务器传递的比客户端的大156字节,当我写这个尖叫到jpeg文件,它无法打开。所以我认为这个流已经包含了信息。我检查了AFNETwork的源代码,我发现这些代码:
- (void)appendPartWithFileData:(NSData *)data
name:(NSString *)name
fileName:(NSString *)fileName
mimeType:(NSString *)mimeType
{
NSParameterAssert(name);
NSParameterAssert(fileName);
NSParameterAssert(mimeType);
NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
[mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
[mutableHeaders setValue:mimeType forKey:@"Content-Type"];
[self appendPartWithHeaders:mutableHeaders body:data];
}
- (void)appendPartWithHeaders:(NSDictionary *)headers
body:(NSData *)body
{
NSParameterAssert(body);
AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
bodyPart.stringEncoding = self.stringEncoding;
bodyPart.headers = headers;
bodyPart.boundary = self.boundary;
bodyPart.bodyContentLength = [body length];
bodyPart.body = body;
[self.bodyStream appendHTTPBodyPart:bodyPart];
}
我认为它设置了文件名和图像数据,它们都已设置到流中。
我们可以从这个流中获取这些信息吗?
我的wcf主代码,其他和这个question一样
public void SaveImage(Stream request)
{
Exception e = new Exception(string.Format(" begin to save"));
WcfLog.Log(logLevel.Info, e);
string upLoadFolder = @"C:\images";
string fileName ="testpig.jpg";
string filePath = null;
Stream sourceStream = request;
FileStream targetStream = null;
if (!sourceStream.CanRead)
{
WcfLog.Log( "can not read this stream!");
}
if (filePath == null) filePath = @"otherFile\";
if (!filePath.EndsWith("\\")) filePath += "\\";
if (!upLoadFolder.EndsWith("\\")) upLoadFolder += "\\";
upLoadFolder = upLoadFolder + filePath;
if (!Directory.Exists(upLoadFolder))
{
Directory.CreateDirectory(upLoadFolder);
}
int filesize = 0;
string filePathAndName = Path.Combine(upLoadFolder, fileName);
try
{
using (targetStream = new FileStream(filePathAndName, FileMode.Create, FileAccess.Write, FileShare.None))
{
const int bufferLen = 4096;
Byte[] buffer = new Byte[bufferLen];
int count = 0;
while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
{
targetStream.Write(buffer, 0, count);
filesize += count;
}
targetStream.Close();
sourceStream.Close();
WcfLog.Log( string.Format("filename:{0} filesize:{1} save!", fileName, filesize));
}
}
catch (Exception ex)
{
WcfLog.Log(logLevel.Error, ex);
}`
和 Objective-c 代码:
+(void)upLoadImage:(UIImage *)image Urlstring:(NSString *)path name:(NSString *)name successBlock:(void (^)(AFHTTPRequestOperation *,id responseObject))success failureBlcok:(void (^)(AFHTTPRequestOperation *, NSError *))failure processBlock:(void (^)(CGFloat))process{
NSData *imagedata=UIImageJPEGRepresentation(image, 1.0);
if ((float)imagedata.length/1024>1000) {
imagedata=UIImageJPEGRepresentation(image, 1024*1000/(float)imagedata.length);
}
NSLog(@"imagedata size :%lu",imagedata.length);
NSString *filename=@"test.jpg";
AFHTTPRequestOperationManager *AFManager=[[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@FileTranUrl]];
AFHTTPRequestOperation *operation=[AFManager POST:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formdata){[formdata appendPartWithFileData:imagedata name:name fileName:filename mimeType:@"image/jpeg"];} success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
success(operation,responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(operation,error);
}
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
CGFloat progressValue = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
if (process) {
process(progressValue);
}
}];
[operation start];
}
【问题讨论】:
标签: c# ios wcf afnetworking