Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MySQL-将base64编码数据保存到存储驱动器_C#_Mysql_Sql - Fatal编程技术网

C# MySQL-将base64编码数据保存到存储驱动器

C# MySQL-将base64编码数据保存到存储驱动器,c#,mysql,sql,C#,Mysql,Sql,我正在构建一个应用程序,它使用一个临时表,将文件块上传到临时表,然后调用一个存储的进程,并使用一个函数将文件重新组装成正确的顺序。我只是想知道SQL中是否有任何东西允许我将重新组装的文件直接保存到磁盘(例如c:/path/to/file/filename),或者我是否需要让应用程序读取从存储过程返回的重新组装的文件并保存文件 无论哪种方式,我都会得到我需要的最终结果,只是想知道我是否可以让数据库完成所有的工作。。。或者这是否是个好主意 编辑:我选择了另一种方法。没有任何内容保存到数据库中。文件在

我正在构建一个应用程序,它使用一个临时表,将文件块上传到临时表,然后调用一个存储的进程,并使用一个函数将文件重新组装成正确的顺序。我只是想知道SQL中是否有任何东西允许我将重新组装的文件直接保存到磁盘(例如c:/path/to/file/filename),或者我是否需要让应用程序读取从存储过程返回的重新组装的文件并保存文件

无论哪种方式,我都会得到我需要的最终结果,只是想知道我是否可以让数据库完成所有的工作。。。或者这是否是个好主意


编辑:我选择了另一种方法。没有任何内容保存到数据库中。文件在服务器上的最终存储位置重新组装。解决方案如下。

我想用我的解决方案更新这篇文章:

我为上传的每个文件生成了一个GUID客户端,并将其用作每个区块的文件名,并以区块顺序ID作为前缀,以便服务器知道文件需要以何种顺序重新组装。每个区块都会发布到服务器上的最终存储位置。我将要上载的总块数与服务器上的总块数进行比较,并在接收到最后一个块时进行检测,然后将文件重新编译为原始文件。任何需要的验证都是在上传的文件重新组装后完成的。如果任何时候验证或上传失败,文件和任何块都会被删除

下面的代码是从我的原始代码编辑而来的,未经测试。这可能需要清理一些

这里没有显示客户端JavaScript,它使用FileReader API在base64中发布块

        [HttpPost("chunk-upload")]
    public async Task<IActionResult> ChunkUpload(
        string chunk, 
        int chunkId, 
        bool isLastChunk, 
        string fileName, 
        int totalSize, 
        string uuid)
    {
        byte[] fileAsByteArray;

        try
        {
            fileAsByteArray = Convert.FromBase64String(chunk);
        }
        catch (Exception)
        {
            // delete chunks associated with the file if upload fails
            string[] fileEntries = Directory.GetFiles("your/folder/path/").Where(x => x.Contains(uuid)).OrderBy(x => x).ToArray();
            for(int i = 0; i < fileEntries.Count(); i++)
            {
                System.IO.File.Delete(fileEntries[i]);
            }
        
            return Json(new { error = "Error uploading file, please try again" });
        }

        string saveChunkLocation = "your/folder/path/" + string.Format("{0:0000}_{1}_{2}.mp3", chunkId, uuid)));

        byte[] buffer = new byte[1024 * 1024];

        using (FileStream stream = System.IO.File.Create(saveChunkLocation))
        {                
            await stream.WriteAsync(fileAsByteArray, 0, fileAsByteArray.Length);
        }

        if(isLastChunk)
        {
            MyFileWriterHelper fw = new MyFileWriterHelper();

            string[] fileEntries = Directory.GetFiles("your/folder/path/").Where(x => x.Contains(uuid)).OrderBy(x => x).ToArray();

            byte[][] fileChunks = new byte[fileEntries.Count()][];
            
            for(int i = 0; i < fileEntries.Count(); i++)
            {
                // get file bytes then delete chunk from storage
                fileChunks[i] = System.IO.File.ReadAllBytes(fileEntries[i]);
                System.IO.File.Delete(fileEntries[i]);
            }

            byte[] completeFile = fw.Combine(fileChunks);
            if(completeFile.Length == totalSize)
            {
                var fileFullDestinationPath = "your/folder/path/" + fileName;
                using (FileStream SourceStream = System.IO.File.Create(fileFullDestinationPath))
                {
                    await SourceStream.WriteAsync(completeFile, 0, completeFile.Length);
                }
                
                // Validate file here, using fileFullDestinationPath to pass into whatever validator you're using
                
            }
            else
            {
                return Json(new { error = "Error uploading file, please try again" });
            }
        }

        return Json(new { success = "Upload complete" });
    }
[HttpPost(“区块上传”)]
公共异步任务块上载(
字符串块,
int chunkId,
布尔·伊斯兰斯彻克,
字符串文件名,
整数总大小,
字符串(uuid)
{
字节[]fileAsByteArray;
尝试
{
fileAsByteArray=Convert.FromBase64String(块);
}
捕获(例外)
{
//如果上载失败,请删除与文件关联的块
string[]fileEntries=Directory.GetFiles(“您的/folder/path/”)。其中(x=>x.Contains(uuid)).OrderBy(x=>x.ToArray();
对于(int i=0;ix.Contains(uuid)).OrderBy(x=>x.ToArray();
byte[][]fileChunks=新字节[fileEntries.Count()][];
对于(int i=0;i
问题到底是什么?能做到吗?或者是这样或那样更好(完全不同的问题)。这是可以做到的,但是当你有一个数据库可以使用时,真的有理由这样做吗?虽然我有一个数据库,但我不希望数据库永久保留文件数据。我将要存储大文件,数据库将很快填满我VPS上的空间,因此我正在实施的分块上传方法需要将文件移动到块存储。我的问题分为两部分。你用“可以做到”回答了一部分。你能举个例子吗?我可能没有搜索正确的单词/短语,也可能是我用了错误的方法,但到目前为止,我还没有找到任何SQL将文件保存到另一个存储位置的示例。