1

I write an ios client to send *.jpg to the server, the server used WCF. Now I can get the stream from the client, but the problem is that I don't know where to get the filename and mimeType. And another problem is that I found the stream has been passed to the server is bigger than the one in the client, when I did test, I found the server has been passed was larger than the one from client 156 byte, and when I write this scream to jpeg file,it can't be opened. So I think this stream has included the information.I check the source code of AFNETwork, I find these code:

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

I think it set the file name and the image data all of them have been set to the stream.

Can we get this information from this stream?

my wcf main code, other is same with this 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);

        }`

and the Objective-c code:

+(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];
}
Community
  • 1
  • 1

1 Answers1

0

Now I know the problem which I meet. I need to encode the stream and split it find the key work I need. I am sure that the afnetwork has sent the content-type,filename into the stream. I find this poster:here

if you want to see more problem like this,Google the key word: multipart/form-data and wcf

Community
  • 1
  • 1