2011-04-01 15 views
11

मैं उस सिस्टम के साथ संवाद करने की कोशिश कर रहा हूं जिस पर मेरा कोई नियंत्रण नहीं है, हालांकि इसके किसी भी तरीके से HttpPostedFile में मेरे कोड में एक बाइट सरणी है। क्या किसी के पास HttpPostedFile को तुरंत चालू करने का एक उदाहरण है क्योंकि मुझे पता है कि यह कन्स्ट्रक्टर आंतरिक है?एक HttpPostedFile को तुरंत चालू करने के लिए

सबसे अच्छा मुझे मिला है this SO post जो प्रतिबिंब का उपयोग करता है हालांकि उन्हें दूसरी दिशा में चलाया गया था जिसे मैं नहीं ले सकता क्योंकि मैं तीसरे पक्ष के सिस्टम विधि हस्ताक्षर को संशोधित करने में असमर्थ हूं।

उत्तर

15

यह वास्तव में सच में hacky कोड है, लेकिन निम्न मेरे लिए काम करने लगता है:

public HttpPostedFile ConstructHttpPostedFile(byte[] data, string filename, string contentType) { 
    // Get the System.Web assembly reference 
    Assembly systemWebAssembly = typeof (HttpPostedFileBase).Assembly; 
    // Get the types of the two internal types we need 
    Type typeHttpRawUploadedContent = systemWebAssembly.GetType("System.Web.HttpRawUploadedContent"); 
    Type typeHttpInputStream = systemWebAssembly.GetType("System.Web.HttpInputStream"); 

    // Prepare the signatures of the constructors we want. 
    Type[] uploadedParams = { typeof(int), typeof(int) }; 
    Type[] streamParams = {typeHttpRawUploadedContent, typeof (int), typeof (int)}; 
    Type[] parameters = { typeof(string), typeof(string), typeHttpInputStream }; 

    // Create an HttpRawUploadedContent instance 
    object uploadedContent = typeHttpRawUploadedContent 
    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, uploadedParams, null) 
    .Invoke(new object[]{data.Length, data.Length}); 

    // Call the AddBytes method 
    typeHttpRawUploadedContent 
    .GetMethod("AddBytes", BindingFlags.NonPublic | BindingFlags.Instance) 
    .Invoke(uploadedContent, new object[] {data, 0, data.Length}); 

    // This is necessary if you will be using the returned content (ie to Save) 
    typeHttpRawUploadedContent 
    .GetMethod("DoneAddingBytes", BindingFlags.NonPublic | BindingFlags.Instance) 
    .Invoke(uploadedContent, null); 

    // Create an HttpInputStream instance 
    object stream = (Stream)typeHttpInputStream 
    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, streamParams, null) 
    .Invoke(new object[] {uploadedContent, 0, data.Length}); 

    // Create an HttpPostedFile instance 
    HttpPostedFile postedFile = (HttpPostedFile)typeof(HttpPostedFile) 
    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null) 
    .Invoke(new object[] {filename, contentType, stream}); 

    return postedFile; 
} 
+0

एकदम सही है कि (hacky सहमति है, लेकिन एक बहुत नहीं मैं क्या कर सकते हैं)। इसके लिए धन्यवाद। –

+0

@paracycle - कॉल करते समय .वेट (filePath) वापस लौटे HttpPostedFile - मुझे एक खाली छवि मिलती है। कोई विचार क्यों? – ajwaka

+2

परिणामस्वरूप स्ट्रीम परिणामों से एक त्रुटि में पढ़ना: -/ – wilsjd

4

तुम कोशिश कर सकते

var constructorInfo = typeof(HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0]; 
var obj = (HttpPostedFile)constructorInfo 
      .Invoke(new object[] { "filename", "image/jpeg", null }); 

obj के प्रकार HttpPostedFile .मैं शून्य पर पिछले पैरामीटर लेकिन यह हालांकि एक HttpInputStream हो गया होता।

संबंधित मुद्दे