Click or drag to resize

IContentWriteAsync Method

IT Hit WebDAV Classes Reference
Saves the content of the file from the specified stream to the storage repository.

Namespace:  ITHit.Server
Assembly:  ITHit.Server (in ITHit.Server.dll) Version: 13.3.13068
Syntax
Task<bool> WriteAsync(
	Stream content,
	string contentType,
	long startIndex,
	long totalFileSize
)

Parameters

content
Type: System.IOStream
Stream to read the content of the file from.
contentType
Type: SystemString
Indicates the media type of the file.
startIndex
Type: SystemInt64
Start offset to which content shall be saved.
totalFileSize
Type: SystemInt64
Entire length of the file. Is is not less then length of content stream.

Return Value

Type: TaskBoolean
Boolean value indicating whether entire stream was written. This value is used by the engine to take decision whether autocheckin shall be performed.
Exceptions
ExceptionCondition
LockedExceptionThe file was locked and client did not provide lock token.
NeedPrivilegesExceptionThe user doesn't have enough privileges.
InsufficientStorageExceptionQuota limit is reached.
DavExceptionIn other cases.
Remarks

IIS and ASP.NET does not support files upload larger than 2Gb. If you need to upload files larger than 2Gb you must develop HttpListener-based server or implement resumable upload interfaces.

If you are creating HttpHandler-based server you must specify the file maximum upload size in web.config of your web application. By default maximum upload size is set to 4096 KB (4 MB) by ASP.NET. This limit is used to prevent denial of service attacks caused by users posting large files to the server. To increase the upload limit add <httpRuntime> section to your web application web.config file and specify the limit in kilobytes:

XML
<configuration>
   <system.web>
      ...
      <httpRuntime maxRequestLength="2097151" /> //2Gb
      ...
   </system.web>
</configuration>

When client uploads file to IIS, ASP.NET first creates the file in a the temporary upload directory. Only when the entire file is uploaded to server you can read its content from stream. By default ASP.NET uploads files to %FrameworkInstallLocation%\Temporary ASP.NET Files folder. You must make sure you have enough disk space to keep temporary files uploaded to your server. To change this folder location add the following section to your web.config file:

XML
<configuration>
    <system.web>
       ...
       <compilation tempDirectory="temporary files directory" />
       ...
    </system.web>
</configuration>
To avoid temporary file creation and pass content directly to engine set the ResumableUpload.PutUploadProgressAndResumeModule module in your web.config file. Unlike IIS/ASP.NET, HttpListener-based server does not create any temporary files when handling uploads.

Examples

The code below is part of 'WebDAVServer.FileSystemStorage.AspNet' C# & VB samples provided with the SDK.

public virtual async Task<bool> WriteAsync(Stream content, string contentType, long startIndex, long totalFileSize)
{
    await RequireHasTokenAsync();
    //Set timeout to maximum value to be able to upload large files.
    HttpContext.Current.Server.ScriptTimeout = int.MaxValue;
    await WriteInternalAsync(content, contentType, startIndex, totalFileSize);
    await context.socketService.NotifyUpdatedAsync(Path, GetWebSocketID());
    return true;
}

public async Task<bool> WriteInternalAsync(Stream content, string contentType, long startIndex, long totalFileSize)
{
    if (startIndex == 0 && fileInfo.Length > 0)
    {
        using (FileStream filestream = fileInfo.Open(FileMode.Truncate)) { }
    }
    await fileInfo.SetExtendedAttributeAsync("TotalContentLength", (object)totalFileSize);
    await fileInfo.SetExtendedAttributeAsync("SerialNumber", ++this.serialNumber);

    using (FileStream fileStream = fileInfo.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
    {
        if (fileStream.Length < startIndex)
        {
            throw new DavException("Previous piece of file was not uploaded.", DavStatus.PRECONDITION_FAILED);
        }

        if (!content.CanRead)
        {
            return true;
        }

        fileStream.Seek(startIndex, SeekOrigin.Begin);
        byte[] buffer = new byte[bufSize];

        int lastBytesRead;
        try
        {
            lastBytesRead = await content.ReadAsync(buffer, 0, bufSize);
            while (lastBytesRead > 0)
            {
                await fileStream.WriteAsync(buffer, 0, lastBytesRead);
                lastBytesRead = await content.ReadAsync(buffer, 0, bufSize);
            }
        }
        catch (HttpException)
        {
            // The remote host closed the connection (for example Cancel or Pause pressed).
        }
    }
    return true;
}
Examples

The code below is part of 'WebDAVServer.FileSystemSynchronization.AspNetCore' C# & VB samples provided with the SDK.

C#
public virtual async Task<bool> WriteAsync(Stream content, string contentType, long startIndex, long totalFileSize)
{
    await RequireHasTokenAsync();
    await WriteInternalAsync(content, contentType, startIndex, totalFileSize);
    await context.socketService.NotifyUpdatedAsync(Path, GetWebSocketID());
    return true;
}

public async Task<bool> WriteInternalAsync(Stream content, string contentType, long startIndex, long totalFileSize)
{
    if (startIndex == 0 && fileInfo.Length > 0)
    {
        await using (FileStream filestream = fileInfo.Open(FileMode.Truncate)) { }
    }
    await fileInfo.SetExtendedAttributeAsync("TotalContentLength", (object)totalFileSize);
    await fileInfo.SetExtendedAttributeAsync("SerialNumber", ++this.serialNumber);

    await using (FileStream fileStream = fileInfo.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
    {
        if (fileStream.Length < startIndex)
        {
            throw new DavException("Previous piece of file was not uploaded.", DavStatus.PRECONDITION_FAILED);
        }

        if (!content.CanRead)
        {
            return true;
        }

        fileStream.Seek(startIndex, SeekOrigin.Begin);
        byte[] buffer = new byte[bufSize];

        int lastBytesRead;
        try
        {
            lastBytesRead = await content.ReadAsync(buffer, 0, bufSize);
            while (lastBytesRead > 0)
            {
                await fileStream.WriteAsync(buffer, 0, lastBytesRead);
                lastBytesRead = await content.ReadAsync(buffer, 0, bufSize);
            }
        }
        catch (IOException)
        {
            // Message = "Error -4077 ECONNRESET connection reset by peer"
            // The remote host closed the connection (for example Cancel or Pause pressed).
        }
    }
    return true;
}
See Also