簡體   English   中英

WCF從AFNetworking接收流

[英]WCF Receive stream from AFNetworking

我編寫了一個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主代碼,其他與此問題相同

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

現在我知道了我遇到的問題。 我需要對流進行編碼並將其拆分,以找到所需的關鍵工作。 我確定AFNetwork已將內容類型,文件名發送到流中。 我找到這張海報: 這里

如果您想看到更多類似的問題,請使用Google關鍵字:multipart / form-data和wcf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM