简体   繁体   English

C#FileStream错误地读取字节

[英]C# FileStream reads bytes incorrectly

I am trying to develop an app that will upload large files to a web server running PHP. 我正在尝试开发一个将大文件上传到运行PHP的Web服务器的应用程序。 Almost immediately, I stumbled upon a problem that the file is not split correctly. 几乎立即,我偶然发现了文件未正确拆分的问题。

Currently I have this piece of code 目前我有这段代码

string adrese = "c:\\directory\\file.jpg";
int garums = 16384;
String ext = Path.GetExtension(adrese);

FileStream file = /*/File.Open(adrese, FileMode.Open);/*/
    new FileStream(adrese, FileMode.Open, System.IO.FileAccess.Read);
long fgar = file.Length; //100%
long counter = garums;
first = true;
byte[] chunk = new byte[garums];

while (true)
{
    int index = 0;
    //long Controll = counter+garums;
    while (index < chunk.Length)
    {
        int bytesRead = file.Read(chunk, index, chunk.Length - index);
        if (bytesRead == 0)
        {
            /*byte[] biti = new byte[index];
            for (int i = 0; i < index; i++)
            {
                biti[i] = chunk[i];
            }
            chunk = new byte[index];
            chunk = biti;*/
            break;
        }
        index += bytesRead;
    }

    if (index != 0) // Our previous chunk may have been the last one
    {
        byte[] biti = new byte[index];
        for (int i = 0; i < index; i++)
        {
            biti[i] = chunk[i];
        }
        chunk = new byte[index];
        chunk = biti;
        // index is the number of bytes in the chunk
        sutam(Convert.ToBase64String(chunk),ext);
    }

    double procentuali = ((counter * 100) / fgar);
    if (procentuali > 99)
    {
        procentuali = 100;
    }

    progressBar1.Value = (int)Math.Round(procentuali);
    label1.Text = "" + procentuali;

    counter = counter+garums;
    if (index != garums) // We didn't read a full chunk: we're done
    {
        return;
    }
}

file.Close();

Everything works if I set garums to 1, but who will wait for a year or so to upload a file sized multiple GB's. 如果我将garums设置为1,则一切正常,但是谁将等待一年左右才能上传大小为GB的文件。

I would be pleased if you could tell me what is wrong and how to fix this. 如果您能告诉我什么地方出了问题以及如何解决此问题,我将感到高兴。

Try this instead to upload in chunks: 尝试以下操作以分块上传:

private void ConvertToChunks()  
{
    //Open file
    string file = MapPath("~/temp/1.xps");
    FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
    //Chunk size that will be sent to Server
    int chunkSize = 1024;
    // Unique file name
    string fileName = Guid.NewGuid() + Path.GetExtension(file);
    int totalChunks = (int)Math.Ceiling((double)fileStream.Length / chunkSize);
    // Loop through the whole stream and send it chunk by chunk;
    for (int i = 0; i < totalChunks; i++)
    {
        int startIndex = i * chunkSize;
        int endIndex = (int)(startIndex + chunkSize > fileStream.Length ?   fileStream.Length : startIndex + chunkSize);
        int length = endIndex - startIndex;

        byte[] bytes = new byte[length];
        fileStream.Read(bytes, 0, bytes.Length);
        ChunkRequest(fileName, bytes);
    }
}

private void ChunkRequest(string fileName,byte[] buffer)  
{  
    //Request url, Method=post Length and data.
    string requestURL = "http://localhost:63654/hello.ashx";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";

    // Chunk(buffer) is converted to Base64 string that will be convert to Bytes on  the handler.
    string requestParameters = @"fileName=" + fileName + "&data=" + HttpUtility.UrlEncode( Convert.ToBase64String(buffer) );

    // finally whole request will be converted to bytes that will be transferred to HttpHandler
    byte[] byteData = Encoding.UTF8.GetBytes(requestParameters);

    request.ContentLength = byteData.Length;

    Stream writer = request.GetRequestStream();
    writer.Write(byteData, 0, byteData.Length);
    writer.Close();
    // here we will receive the response from HttpHandler
    StreamReader stIn = new StreamReader(request.GetResponse().GetResponseStream());
    string strResponse = stIn.ReadToEnd();
    stIn.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM