Home > Web Front-end > H5 Tutorial > body text

How to read files in H5 and upload them to the server

php中世界最好的语言
Release: 2018-03-27 13:38:57
Original
5876 people have browsed it

This time I will bring you the method of H5reading filesand uploading them to the server. What are the precautions of the method of H5 reading files and uploading them to the server? The following are Let’s take a look at practical cases.

Note: When uploading using Ajax, the file should not be too large, preferably less than three to four hundred megabytes, because too many consecutive Ajax requests will cause the background to crash and the data in the InputStream will be empty, especially when browsing on Google during device testing.

1. Simply read the file into Blobs in segments and upload it to the server with ajax

<p class="container">
    <p class="panel panel-default">
        <p class="panel-heading">分段读取文件:</p>
        <p class="panel-body">
            <input type="file" id="file" />
            <blockquote style="word-break:break-all;"></blockquote>
        </p>
    </p>
</p>
Copy after login

JS:

/*
* 分段读取文件为blob ,并使用ajax上传到服务器
* 分段上传exe文件会抛出异常
*/
var fileBox = document.getElementById('file');
file.onchange = function () {
    //获取文件对象
    var file = this.files[0];
    var reader = new FileReader();
    var step = 1024 * 1024;
    var total = file.size;
    var cuLoaded = 0;
    console.info("文件大小:" + file.size);
    var startTime = new Date();
    //读取一段成功
    reader.onload = function (e) {
        //处理读取的结果
        var loaded = e.loaded;
        //将分段数据上传到服务器
        uploadFile(reader.result, cuLoaded, function () {
            console.info('loaded:' + cuLoaded + 'current:' + loaded);
            //如果没有读完,继续
            cuLoaded += loaded;
            if (cuLoaded < total) {
                readBlob(cuLoaded);
            } else {
                console.log(&#39;总共用时:&#39; + (new Date().getTime() - startTime.getTime()) / 1000);
                cuLoaded = total;
            }
        });
    }
    //指定开始位置,分块读取文件
    function readBlob(start) {
        //指定开始位置和结束位置读取文件
        //console.info(&#39;start:&#39; + start);
        var blob = file.slice(start, start + step);
        reader.readAsArrayBuffer(blob);
    }
    //开始读取
    readBlob(0);
    //关键代码上传到服务器
    function uploadFile(result, startIndex, onSuccess) {
        var blob = new Blob([result]);
        //提交到服务器
        var fd = new FormData();
        fd.append(&#39;file&#39;, blob);
        fd.append(&#39;filename&#39;, file.name);
        fd.append(&#39;loaded&#39;, startIndex);
        var xhr = new XMLHttpRequest();
        xhr.open(&#39;post&#39;, &#39;../ashx/upload2.ashx&#39;, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                // var data = eval(&#39;(&#39; + xhr.responseText + &#39;)&#39;);
                console.info(xhr.responseText);
                if (onSuccess)
                    onSuccess();
            }
        }
        //开始发送
        xhr.send(fd);
    }
}
Copy after login

Backend code:

/// <summary>
/// upload2 的摘要说明
/// </summary>
public class upload2 : IHttpHandler
{
    LogHelper.LogHelper _log = new LogHelper.LogHelper();
    int totalCount = 0;
    public void ProcessRequest(HttpContext context)
    {
        HttpContext _Context = context;
        //接收文件
        HttpRequest req = _Context.Request;
        if (req.Files.Count <= 0)
        {
            WriteStr("获取服务器上传文件失败");
            return;
        }
        HttpPostedFile _file = req.Files[0];
        //获取参数
        // string ext = req.Form["extention"];
        string filename = req.Form["filename"];
        //如果是int 类型当文件大的时候会出问题 最大也就是 1.9999999990686774G
        int loaded = Convert.ToInt32(req.Form["loaded"]);
        totalCount += loaded;
        string newname = @"F:\JavaScript_Solution\H5Solition\H5Solition\Content\TempFile\";
        newname += filename;
        //接收二级制数据并保存
        Stream stream = _file.InputStream;
        if (stream.Length <= 0)
            throw new Exception("接收的数据不能为空");
        byte[] dataOne = new byte[stream.Length];
        stream.Read(dataOne, 0, dataOne.Length);
        FileStream fs = new FileStream(newname, FileMode.Append, FileAccess.Write, FileShare.Read, 1024);
        try
        {
            fs.Write(dataOne, 0, dataOne.Length);
        }
        finally
        {
            fs.Close();
            stream.Close();
        }
        _log.WriteLine((totalCount + dataOne.Length).ToString());
        WriteStr("分段数据保存成功");
    }
    private void WriteStr(string str)
    {
        HttpContext.Current.Response.Write(str); 
    }
    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
Copy after login

2. Read the file into blobs in sections, and upload it to the server using ajax, adding the stop and continue function operations

<p class="container">
    <p class="panel panel-default">
        <p class="panel-heading">分段读取文件:</p>
        <p class="panel-body">
            <input type="file" id="file" />
            <br />
            <input type="button" value="中止" onclick="stop();" />&emsp;
            <input type="button" value="继续" onclick="containue();" />
            <br />
            <progress id="progressOne" max="100" value="0" style="width:400px;"></progress>
            <blockquote id="Status" style="word-break:break-all;"></blockquote>
        </p>
    </p>
</p>
Copy after login

JS:

/*
* 分段读取文件为blob ,并使用ajax上传到服务器
* 使用Ajax方式提交上传数据文件大小应该有限值,最好500MB以内
* 原因短时间过多的ajax请求,Asp.Net后台会崩溃获取上传的分块数据为空
* 取代方式,长连接或WebSocket
*/
var fileBox = document.getElementById('file');
var reader = null;  //读取操作对象
var step = 1024 * 1024 * 3.5;  //每次读取文件大小
var cuLoaded = 0; //当前已经读取总数
var file = null; //当前读取的文件对象
var enableRead = true;//标识是否可以读取文件
fileBox.onchange = function () {
    //获取文件对象
    file = this.files[0];
    var total = file.size;
    console.info("文件大小:" + file.size);
    var startTime = new Date();
    reader = new FileReader();
    //读取一段成功
    reader.onload = function (e) {
        //处理读取的结果
        var result = reader.result;
        var loaded = e.loaded;
        if (enableRead == false)
            return false;
        //将分段数据上传到服务器
        uploadFile(result, cuLoaded, function () {
            console.info('loaded:' + cuLoaded + '----current:' + loaded);
            //如果没有读完,继续
            cuLoaded += loaded;
            if (cuLoaded < total) {
                readBlob(cuLoaded);
            } else {
                console.log(&#39;总共用时:&#39; + (new Date().getTime() - startTime.getTime()) / 1000);
                cuLoaded = total;
            }
            //显示结果进度
            var percent = (cuLoaded / total) * 100;
            document.getElementById(&#39;Status&#39;).innerText = percent;
            document.getElementById(&#39;progressOne&#39;).value = percent;
        });
    }
    //开始读取
    readBlob(0);
    //关键代码上传到服务器
    function uploadFile(result, startIndex, onSuccess) {
        var blob = new Blob([result]);
        //提交到服务器
        var fd = new FormData();
        fd.append(&#39;file&#39;, blob);
        fd.append(&#39;filename&#39;, file.name);
        fd.append(&#39;loaded&#39;, startIndex);
        var xhr = new XMLHttpRequest();
        xhr.open(&#39;post&#39;, &#39;../ashx/upload2.ashx&#39;, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                if (onSuccess)
                    onSuccess();
            } else if (xhr.status == 500) {
                //console.info(&#39;请求出错,&#39; + xhr.responseText);
                setTimeout(function () {
                    containue();
                }, 1000);
            }
        }
        //开始发送
        xhr.send(fd);
    }
}
//指定开始位置,分块读取文件
function readBlob(start) {
    //指定开始位置和结束位置读取文件
    var blob = file.slice(start, start + step);
    reader.readAsArrayBuffer(blob);
}
//中止
function stop() {
    //中止读取操作
    console.info(&#39;中止,cuLoaded:&#39; + cuLoaded);
    enableRead = false;
    reader.abort();
}
//继续
function containue() {
    console.info(&#39;继续,cuLoaded:&#39; + cuLoaded);
    enableRead = true;
    readBlob(cuLoaded);
}
Copy after login

The background code is the same as above

3. Read the file in segments into a binary array and upload it to the server using ajax

Using binary array transfer is very inefficient, and the final file still deviates somewhat from the original size

HTML content is the same as above

JS:

/*
    * 分段读取文件为二进制数组 ,并使用ajax上传到服务器
    * 使用二进制数组传递的方式,效率特别低,最终文件还与原始大小有些偏差
    */
var fileBox = document.getElementById(&#39;file&#39;);
var reader = new FileReader(); //读取操作对象
var step = 1024 * 1024;  //每次读取文件大小
var cuLoaded = 0; //当前已经读取总数
var file = null; //当前读取的文件对象
var enableRead = true;//标识是否可以读取文件
fileBox.onchange = function () {
    //获取文件对象
    if (file == null) //如果赋值多次会有丢失数据的可能
        file = this.files[0];
    var total = file.size;
    console.info("文件大小:" + file.size);
    var startTime = new Date();
    //读取一段成功
    reader.onload = function (e) {
        //处理读取的结果
        var result = reader.result;
        var loaded = e.loaded;
        if (enableRead == false)
            return false;
        //将分段数据上传到服务器
        uploadFile(result, cuLoaded, function () {
            console.info(&#39;loaded:&#39; + cuLoaded + &#39;----current:&#39; + loaded);
            //如果没有读完,继续
            cuLoaded += loaded;
            if (cuLoaded < total) {
                readBlob(cuLoaded);
            } else {
                console.log(&#39;总共用时:&#39; + (new Date().getTime() - startTime.getTime()) / 1000);
                cuLoaded = total;
            }
            //显示结果进度
            var percent = (cuLoaded / total) * 100;
            document.getElementById(&#39;Status&#39;).innerText = percent;
            document.getElementById(&#39;progressOne&#39;).value = percent;
        });
    }
    //开始读取
    readBlob(0);
    //关键代码上传到服务器
    function uploadFile(result, startIndex, onSuccess) {
        var array = new Int8Array(result);
        console.info(array.byteLength);
        //提交到服务器
        var fd = new FormData();
        fd.append(&#39;file&#39;, array);
        fd.append(&#39;filename&#39;, file.name);
        fd.append(&#39;loaded&#39;, startIndex);
        var xhr = new XMLHttpRequest();
        xhr.open(&#39;post&#39;, &#39;../ashx/upload3.ashx&#39;, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                // console.info(xhr.responseText);
                if (onSuccess)
                    onSuccess();
            } else if (xhr.status == 500) {
                console.info(&#39;服务器出错&#39;);
                reader.abort();
            }
        }
        //开始发送
        xhr.send(fd);
    }
}
//指定开始位置,分块读取文件
function readBlob(start) {
    //指定开始位置和结束位置读取文件
    var blob = file.slice(start, start + step);
    reader.readAsArrayBuffer(blob);
}
//中止
function stop() {
    //中止读取操作
    console.info(&#39;中止,cuLoaded:&#39; + cuLoaded);
    enableRead = false;
    reader.abort();
}
//继续
function containue() {
    console.info(&#39;继续,cuLoaded:&#39; + cuLoaded);
    enableRead = true;
    readBlob(cuLoaded);
}
Copy after login

Backend code:

/// <summary>
/// upload3 的摘要说明
/// </summary>
public class upload3 : IHttpHandler
{
    LogHelper.LogHelper _log = new LogHelper.LogHelper();
    int totalCount = 0;
    public void ProcessRequest(HttpContext context)
    {
        HttpContext _Context = context;
        //接收文件
        HttpRequest req = _Context.Request;
        string data = req.Form["file"];
        //转换方式一
        //int[] intData = data.Split(',').Select(q => Convert.ToInt32(q)).ToArray();
        //byte[] dataArray = intData.ToList().ConvertAll(x=>(byte)x).ToArray();
        //转换方式二
        byte[] dataArray = data.Split(',').Select(q => int.Parse(q)).Select(q => (byte)q).ToArray();
        //获取参数
        string filename = req.Form["filename"];
        //如果是int 类型当文件大的时候会出问题 最大也就是 1.9999999990686774G
        int loaded = Convert.ToInt32(req.Form["loaded"]);
        totalCount += loaded;
        string newname = @"F:\JavaScript_Solution\H5Solition\H5Solition\Content\TempFile\";
        newname += filename;
        try
        {
            // 接收二级制数据并保存
            byte[] dataOne = dataArray;
            FileStream fs = new FileStream(newname, FileMode.Append, FileAccess.Write, FileShare.Read, 1024);
            try
            {
                fs.Write(dataOne, 0, dataOne.Length);
            }
            finally
            {
                fs.Close();
            }
            _log.WriteLine((totalCount + dataOne.Length).ToString());
            WriteStr("分段数据保存成功");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void WriteStr(string str)
    {
        HttpContext.Current.Response.Write(str);
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
Copy after login

I believe you have mastered the method after reading the case in this article, and there will be more exciting things Please pay attention to other related articles on php Chinese website!

Recommended reading:

How to achieve the animation effect of picture rotation in html5

H5 implements desktop notification

The above is the detailed content of How to read files in H5 and upload them to the server. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!