2011-10-17 6 views
6

मैं डिस्क पर बाहर अपलोड की गई छवि लिखने के लिए नए एएसपीनेट 4.5 एसिंक हैंडलर के साथ-साथ Request.GetBufferlessInputStream का उपयोग करने का प्रयास करने का प्रयास कर रहा हूं। यह कोड चलता है और यह एक फाइल लिखता है लेकिन छवि दूषित है और मुझे यकीन नहीं है कि क्यों। यहां कोड है जिसका उपयोग मैं.NET 4.5 HttpTaskAsyncHandler फ़ाइल अपलोड करें

public class UploadHandler : HttpTaskAsyncHandler 
{ 
    public override Task ProcessRequestAsync(HttpContext context) 
    { 
     // Gets a Stream object that can be used to read the 
     // incoming HTTP entity body, optionally disabling the 
     // request-length limit that is set in the MaxRequestLength property. 

     // This method provides an alternative to using the 
     // InputStream property. The InputStream property waits until the 
     // whole request has been received before it returns a Stream object. 
     // In contrast, the GetBufferlessInputStream method returns 
     // the Stream object immediately. 
     // You can use the method to begin processing the 
     // entity body before the complete contents of the 
     // body have been received. 
     // The entity body (or as much of it as you request and has 
     // been received) is returned only when you use the object that 
     // is returned by this method to read the stream, by calling 
     // methods such as the Read method. You use parameters of the 
     // Read method to specify how much of the entity body to read. 

     // This method can be useful if the request is uploading a 
     // large file and you want to begin accessing the file contents 
     // before the upload is finished. 
     // However, you should only use this method for scenarios where 
     // you want to take over all processing of the entity body. 
     // This means that you cannot use this method from an .aspx page, 
     // because by the time an .aspx page runs, the entity body 
     // has already been read. 

     using (Stream input = context.Request.GetBufferlessInputStream(true)) 
     using (var file = new FileStream("C:\\myfile.jpg", FileMode.Create, 
      FileAccess.Write, FileShare.Write)) 
     { 
      input.CopyTo(file); 
     } 

     context.Response.ContentType = "text/plain"; 
     return context.Response.Output.WriteAsync("Done"); 
    } 
} 
+1

कैसे यह भ्रष्ट है? – Amy

+0

उपरोक्त के आगे, इसे एक बड़ी टेक्स्ट फ़ाइल के साथ आज़माएं। इस तरह परिणाम परिणाम पहुंच योग्य होगा लेकिन आप देख सकते हैं कि फ़ाइल में क्या हो रहा है। – stevenrcfox

+0

मुझे लगता है कि मुझे वास्तविक अनुरोध का विश्लेषण करना होगा और फॉर्म/मल्टीपार्ट डेटा की तलाश करनी होगी? – superlogical

उत्तर

0

वैसे ऐसा लगता है कि अब एएसपी.NET वेब एपीआई का उपयोग करके इसे संभालने का एक आसान तरीका है!

इसे यहाँ के बारे में पढ़ें: http://blogs.msdn.com/b/henrikn/archive/2012/03/01/file-upload-and-asp-net-web-api.aspx

उपयोग इन बुरा लड़कों में से एक MultipartFormDataStreamProvider

0

वास्तव में आपके कोड को आजमाने की कोशिश नहीं कर रहा हूं, मैंने एक चीज़ देखी है। क्या आपके Response.ContentType = image/gif को स्ट्रीम को नियमित धारा के बजाय बाइनरीस्ट्रीम नहीं होना चाहिए क्योंकि यह एक ऐसी छवि है जिसके साथ आप काम कर रहे हैं ..?

+0

Response.ContentType टेक्स्ट/सादा होना चाहिए क्योंकि मैं ब्राउज़र के जवाब के रूप में "पूर्ण" लेखन को अतुल्य रूप से लिख रहा हूं। – superlogical