2015-07-20 36 views
0

我希望能夠改變這個代碼,這樣我就不必從文件系統上的文件拉牽引,而是使用一個base64值,保存在數據庫中。有沒有人對StreamContent有足夠的瞭解,知道我需要做什麼來完成這個任務?的Base64到StreamContent上傳圖像,而從文件系統

該文件是一個JPEG文件。

private static StreamContent FileMultiPartBody(string fullFilePath) 
    { 

     var fileInfo = new FileInfo(fullFilePath); 

     var fileContent = new StreamContent(fileInfo.OpenRead()); 

     // Manually wrap the string values in escaped quotes. 
     fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") 
     { 
      FileName = string.Format("\"{0}\"", fileInfo.Name), 
      Name = "\"signature\"", 
     }; 
     fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); 

     return fileContent; 
    } 
+0

看起來像'StreamContent'構造函數接受'Stream'。所以你只需要從你的字符串創建一個流。也許http://stackoverflow.com/questions/1879395/how-to-generate-a-stream-from-a-string能幫忙嗎? –

回答

0

StreamContent只是另一個流(流從fileInfo.OpenRead()在你的例子返回)的包裝。您只需用數據庫中的流替換該流並返回即可。你也可以用Path.GetFileName(fullFilePath)調用替換fileInfo.Name

private Stream GetStreamFromDatabase(string fullFilePath) 
{ 
    //TODO 
} 

private static StreamContent FileMultiPartBody(string fullFilePath) 
{ 

    var fileContent = new StreamContent(GetStreamFromDatabase(fullFilePath)) 

    // Manually wrap the string values in escaped quotes. 
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") 
    { 
     FileName = string.Format("\"{0}\"", Path.GetFileName(fullFilePath)), 
     Name = "\"signature\"", 
    }; 
    fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); 

    return fileContent; 
} 

如果您需要幫助將base64值從數據庫轉換爲流,我會建議詢問一個單獨的問題。

相關問題