Upload Large File Using Box Dicom
BoxDICOM is a medical imaging platform that allows users
to store, manage, and share medical images and related files. In BoxDICOM,
files and folders are organized in a hierarchical structure, similar to a
traditional file system.
Users can upload files and create folders in their
BoxDICOM account to organize their medical images and related files. BoxDICOM
supports a wide range of file formats, including DICOM files, PDFs, JPEGs, and
many others.
Users can also share files and folders with other users,
either by granting them access to specific files or by inviting them to
collaborate on a folder. Sharing can be customized with various permission
levels, such as view-only, edit, or full access.
In addition to basic file management features, BoxDICOM
also includes advanced features such as version control, which allows users to
track changes made to a file over time, and metadata management, which allows
users to add and edit custom metadata for their files.
BoxDICOM also integrates with various medical imaging
applications, such as PACS systems, allowing users to easily import and export
medical images and related files.
Overall, BoxDICOM provides a secure and flexible platform
for managing and sharing medical images and related files, with a range of
features designed to meet the needs of medical professionals and organizations.
Concept of File ID and Folder ID:
In BoxDICOM, both the file ID and folder ID are
represented as strings.
The file ID is a unique identifier for a specific file
stored in BoxDICOM. It is a string of characters that is automatically
generated by the BoxDICOM system when the file is uploaded to the platform. The
file ID can be used to retrieve, update, or delete the file.
The folder ID is a unique identifier for a specific
folder in BoxDICOM. It is also a string of characters that is automatically
generated by the BoxDICOM system when the folder is created. The folder ID can
be used to retrieve, update, or delete the folder, as well as to add or remove
files from the folder.
Both the file ID and folder ID are used as parameters in
various API calls to interact with files and folders in BoxDICOM. They are
typically represented as alphanumeric strings of varying length, depending on
the specific implementation of the BoxDICOM system.
PreflightRequest:
In
BoxDICOM, a preflight request is a type of request that is sent by the client
application to the server to check if the actual request (e.g., a POST request
to upload a file) is allowed by the server, based on the server's Cross-Origin
Resource Sharing (CORS) policy.
A preflight request is an HTTP OPTIONS request that is
sent before the actual request. The purpose of the preflight request is to ask
the server if the actual request can be made. The server responds with headers
that indicate whether the actual request is allowed or not.
The preflight request includes an
Access-Control-Request-Method header that specifies the HTTP method (e.g. POST)
of the actual request, and an Access-Control-Request-Headers header that
specifies the headers that will be included in the actual request.
The server's response to the preflight request includes
Access-Control-Allow-Origin headers that indicate whether the client's domain
is allowed to make requests to the server, and Access-Control-Allow-Methods
headers that indicate which HTTP methods are allowed. The server may also
include other headers, such as Access-Control-Allow-Headers and
Access-Control-Max-Age.
Overall, the preflight request is a mechanism used to
ensure that the client application is authorized to make the actual request to
the server.
Chunk Upload:
In BoxDICOM, chunk upload is a method for uploading large
files to the server in smaller pieces, or "chunks". This allows for
more reliable and efficient transfers, as well as the ability to resume a
failed upload from the point where it left off.
When performing a chunk upload in BoxDICOM, the client
application breaks the file into smaller chunks and sends them to the server
one at a time. Each chunk is accompanied by a range header that specifies the
starting and ending byte positions of the chunk within the entire file.
The server receives each chunk and stores it temporarily
until all the chunks are received. Once all the chunks have been received, the
server assembles them into the complete file.
During the upload process, the client application may
periodically send a check request to the server to verify which chunks have
been successfully received and which are missing. This allows the client to
keep track of the upload progress and to resume the upload from the last
successfully uploaded chunk if necessary.
Chunk upload in BoxDICOM can be configured with various
options, such as the size of each chunk, the number of parallel connections to
the server, and the amount of time allowed for a single chunk upload. These
options can be adjusted based on the specific requirements of the client
application and the network conditions.
Create Folder in BoxDciom:
Here we will use Box Service Account for connecting to
Box Dicom. To connect to Box Dicom we will need “Box Configuration”
using Box.V2;
using Box.V2.Exceptions;
using Box.V2.Models;
using Box.V2.Models.Request;
string boxConfig; // read box config file and assign it
BoxJwt boxJwt = new
BoxJwt(jsonString);
boxJwt.BoxUserClient = boxJwt.GetBoxUserClient(“UserId”);
// UserId is optional
BoxFolder boxFolder;
try
{
var boxFolderRequest = new BoxFolderRequest()
{
Name = FolderName,
Parent = new
BoxRequestEntity()
{
Id = ParentFolderId
},
};
boxFolder=await boxJwt.BoxUserClient.FoldersManager.CreateAsync(boxFolderRequest);
}
catch
(BoxConflictException<BoxFolder> ex)
{
boxFolder =
ex.ConflictingItems.FirstOrDefault(x => x.Name == FolderName);
}
If you want to create folder in root, then
“ParentFolderId” should be “0”.
If you want to create folder in particular
folder, then you have to pass the “FolderId” of that Folder. Box Dicom works
through “FolderId” and “FileId”. When folder is created in BoxDicom FolderId is
assigned to it.
Upload Large File:
string boxConfig; // read box config file and assign it.
BoxJwt boxJwt = new
BoxJwt(jsonString);
boxJwt.BoxUserClient = boxJwt.GetBoxUserClient(“UserId”);
// UserId is optional
string filepath=”Path of the file that you want to
upload”;
FileInfo fileInfo = new
FileInfo(filepath);
int timesec = (int)(fileInfo.Length
/ 70000);
if (timesec < 180) timesec = 180;
const long
CHUNKED_UPLOAD_MINIMUM = 20000000;
BoxPreflightCheckRequest preflightRequest = null;
try
{
preflightRequest = new BoxPreflightCheckRequest
{
Name
= fileInfo.Name,
Size
= fileInfo.Length,
Parent
= new
BoxRequestEntity
{
Id = parentFolderId
}
};
}
catch (Exception ex)
{ }
try
{
using (FileStream toUpload = new FileStream(filePath, FileMode.Open))
{
try
{
try
{
BoxPreflightCheck preflightCheck=await boxJwt.BoxUserClient.FilesManager.PreflightCheck(preflightRequest);
if (toUpload.Length < CHUNKED_UPLOAD_MINIMUM)
{
using (SHA1 sha1 = SHA1.Create())
{
var fileUploadRequest = new BoxFileRequest
{
Name
= filename,
Parent
= new
BoxRequestEntity
{
Id = parentFolderId
}
};
var fileSHA =
sha1.ComputeHash(toUpload);
newFile = await boxJwt.BoxUserClient.FilesManager.UploadAsync(fileRequest: fileUploadRequest, stream: toUpload, contentMD5: fileSHA, timeout: new TimeSpan(0, 0, timeOutSec));
}
}
else
{
newFile = await boxJwt.BoxUserClient.FilesManager.UploadUsingSessionAsync(stream: toUpload, fileName: filename, folderId: parentFolderId, timeout: new TimeSpan(0, 0, timeOutSec));
}
catch
(BoxPreflightCheckConflictException<BoxFile> e)
{
if (e.Message.Contains("item_name_in_use"))
{
//
file already present
}
else
{
//
Exception
in PreflightCheck
}
if (toUpload.Length < CHUNKED_UPLOAD_MINIMUM)
{
using (SHA1 sha1 = SHA1.Create())
{
var fileSHA = sha1.ComputeHash(toUpload);
newFile = await boxJwt.BoxUserClient.FilesManager.UploadNewVersionAsync(fileName:
e.ConflictingItem.Name, fileId:
e.ConflictingItem.Id, stream: toUpload, contentMD5: fileSHA, timeout: new TimeSpan(0, 0,
timeOutSec));
}
}
else
{
newFile = await boxJwt.BoxUserClient.FilesManager.UploadNewVersionUsingSessionAsync(fileId:
e.ConflictingItem.Id, stream: toUpload,
timeout: new TimeSpan(0, 0, timeOutSec));
}
}
catch (Exception e)
{
}
}
catch (Exception ex)
{
}
}
}
catch (Exception ex)
{
}
No comments:
Post a Comment