2011-02-02 5 views
1

파일을 업로드하려고합니다. 그러나 인터넷에서 찾은 많은 자료는 파일을 서버의 폴더에 저장하는 방법을 설명하지만, 필자의 솔루션에는 버퍼링 만 필요했습니다. 가능한가? 파일을 서버 버퍼에 업로드 한 다음 읽고 읽고 정리합니다.ASP.NET에서 버퍼링 할 파일 업로드 방법

Obs : 저는 telerik 구성 요소를 사용하고 있으며 Excel 파일을 읽거나 가져올 필요가 있습니다.

감사

+0

내가 그래서 버퍼에 퍼팅 그들은 작은 파일 좋은 옵션 – anishMarokey

+0

, 그리고 내가 그 최선의 해결책 아니다 참조하십시오 폴더로 발생 같아요. 그러나 당신의 의견은 무엇입니까? @anishMarokey –

답변

2

당신이 이진 스트림을 사용하는 경우 그래서, 방법은 실행의 맥락에 관계없이 그것을 버퍼링 (윈폼 대 asp.net 또는 무엇이든)는 매우 일반적이다 : 또한

public static byte[] ReadFile(string filePath) 
{ 
    byte[] buffer; 
    FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); 
    try 
    { 
    int length = (int)fileStream.Length; // get file length 
    buffer = new byte[length];   // create buffer 
    int count;       // actual number of bytes read 
    int sum = 0;       // total number of bytes read 

    // read until Read method returns 0 (end of the stream has been reached) 
    while ((count = fileStream.Read(buffer, sum, length - sum)) > 0) 
     sum += count; // sum is a buffer offset for next reading 
    } 
    finally 
    { 
    fileStream.Close(); 
    } 
    return buffer; 
} 

, 체크 아웃이 :

http://www.yoda.arachsys.com/csharp/readbinary.html

디폴트는 FileUpload 구성 요소는 당신에게 Byte[]로 파일을 제공합니다
0

- 당신은 단지 MemoryStream에 포장 수 :

var strm = new MemoryStream(fileUpload.PostedFile.Contents); 
    // I think its PostedFile.Contents, something like that anyway 
관련 문제