如何修改服务器端函数以使用jQuery将文件信息返回给客户端?

时间:2019-04-10 18:50:52

标签: jquery .net jquery-file-upload ihttphandler

我有以下代码来查询现有文件信息,它正在结果中寻找文件对象。

$.getJSON('/Upload/UploadHandler.ashx', { f: data.files[0].name, context: data.context, t: e.timeStamp }, function (result) {
            var uuid = Math.random().toString(36).substr(2, 8);         // new uuid to prevent overwriting of file chunks with same name
            var existing = false;
            if (result.files.length !== 0) {

                var file = result.files[0];
                data.uploadedBytes = file && file.size;                 // set the length of bytes already uploaded
                if (file.extra.chunkInfo !== null) {                     // chunkInfo = null if uploaded file is not chunked
                    uuid = file.extra.chunkInfo.uuid;                   // optional: Get existing uuid if uploaded file is chunked
                }
                existing = (file.size === data.files[0].size);           // true, if file already fully uploaded
            }

            if (existing) {                                             // Add file only if it is not uploaded already
                alert("File already uploaded");
            } else {
                data.formData = { 'uuid': uuid };                       // Optional: Add uuid to chunk path to prevent overwriting
                $.blueimp.fileupload.prototype
                    .options.add.call(that, e, data);                   // Add file to list if is not already uploaded
            }

        });

我具有下面的服务器端功能,该功能将文件写入响应对象。我只看到将整个文件写入响应的WriteFile方法 但是我只需要文件大小和chunkInfo作为客户端功能正在寻找。如何修改此功能以使其与上面的脚本一起使用? 谢谢!

private void DeliverFile(HttpContext context)
    {
        var filename = context.Request["f"];
        var filePath = StorageRoot + filename;

        if (File.Exists(filePath))
        {
            context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
            context.Response.ContentType = "application/octet-stream";
            context.Response.ClearContent();
            context.Response.WriteFile(filePath);
        }
        else
            context.Response.StatusCode = 404;
    }

0 个答案:

没有答案