Click or drag to resize

IContentWrite Method

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

Namespace: ITHit.WebDAV.Server
Assembly: ITHit.WebDAV.Server (in ITHit.WebDAV.Server.dll) Version: 4.5.3121.0
Syntax
bool Write(
	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: Boolean
Boolean value indicating whether entire stream was written. This value is used by 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 WebDAV server or implement resumable upload interfaces.

If you are creating HttpHandler-based WebDAV 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:

<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:

<configuration>
    <system.web>
       ...
       <compilation tempDirectory="temporary files directory" />
       ...
    </system.web>
</configuration>
To avoid temporary file creation and pass content directly to engine set the 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.NtfsStorage' sample provided with the SDK.

public virtual bool Write(Stream content, string contentType, long startIndex, long totalFileSize)
{
    RequireHasToken();
    //Set timeout to maximum value to be able to upload large files.
    HttpContext.Current.Server.ScriptTimeout = int.MaxValue;
    return context.FileOperation<bool>(
        this,
        () => writeInternal(content, startIndex, totalFileSize),
        Privilege.Write);
}

private bool writeInternal(Stream content, long startIndex, long totalLength)
{
    if (startIndex == 0 && fileInfo.Length > 0)
    {
        SafeNativeMethods.TruncateFile(fileInfo);
    }
    RewriteStream("SerialNumber", GetStreamAndDeserialize<int>("SerialNumber", context.Logger) + 1);

    using (var 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);
        }

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

        int lastBytesRead;
        try
        {
            while ((lastBytesRead = content.Read(buffer, 0, bufSize)) > 0)
            {
                fileStream.Write(buffer, 0, lastBytesRead);
                fileStream.Flush();
            }
        }
        catch (HttpException)
        {
            // The remote host closed the connection (for example Cancel or Pause pressed).
        }
    }

    return true;
}
See Also