IResource.SaveFromStream Method 

IT Hit WebDAV for .NET

Saves the content of the resource from the specified stream to the WebDAV repository.

[Visual Basic]
Function SaveFromStream( _
   ByVal content As Stream, _
   ByVal contentType As String _
) As WebDAVResponse
[C#]
WebDAVResponse SaveFromStream(
   Stream content,
   string contentType
);

Parameters

content
Stream to read the content of the resource from.
contentType
Indicates the media type of the resource.

Return Value

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.

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>
Unlike IIS/ASP.NET, HttpListener-based server does not create any temporary files when handling uploads.

Example

public WebDAVResponse SaveFromStream(Stream stream, string contentType)
{
    if (!ClientHasToken)
        return new LockedResponse();

    WebDAVResponse resp;

    SqlConnection conn = new SqlConnection(connStr);
    SqlCommand cmd;
    SqlTransaction trans = null;
    conn.Open();
    
    try
    {
        trans = conn.BeginTransaction();
        cmd = conn.CreateCommand();
        cmd.Transaction = trans;
        
        if (stream.Length > 0)
        {
            int bufSize = 4096;
            
            cmd.CommandText = "UPDATE Repository SET"
                + " Modified = GETUTCDATE(),"
                + " Content = NULL,"
                + " ContentType = @ContentType"
                + " WHERE ID = @ID;"
                + "SELECT @Pointer = TEXTPTR(Content) FROM Repository WHERE ID = @ID";

            cmd.Parameters.Add("@ContentType", SqlDbType.NVarChar).Value = contentType;
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
            
            SqlParameter ptrParm = cmd.Parameters.Add("@Pointer", SqlDbType.Binary, 16);
            ptrParm.Direction = ParameterDirection.Output;
            
            cmd.ExecuteNonQuery();

            cmd.CommandText = "UPDATETEXT Repository.Content @Pointer @Offset NULL @Bytes";

            cmd.Parameters.Clear();
            cmd.Parameters.Add("@Pointer", SqlDbType.Binary, 16).Value = ptrParm.Value;
            SqlParameter offsetParm = cmd.Parameters.Add("@Offset", SqlDbType.Int);
            SqlParameter bytesParm = cmd.Parameters.Add("@Bytes", SqlDbType.Image, bufSize);

            BinaryReader br = new BinaryReader(stream);
            
            byte[] buf = br.ReadBytes(bufSize);
            int offset_ctr = 0;
            
            while (buf.Length > 0)
            {
                bytesParm.Value = buf;
                offsetParm.Value = offset_ctr;
                cmd.ExecuteNonQuery();
                offset_ctr += buf.Length;
                buf = br.ReadBytes(bufSize);
            }

            br.Close();

            resp = new OkResponse();
        }
        else
        {
            cmd.CommandText = "UPDATE Repository SET"
                + " Modified = GETUTCDATE(),"
                + " Content = NULL,"
                + " ContentType = @ContentType"
                + " WHERE ID = @ID";
            
            cmd.Parameters.Add("@ContentType", SqlDbType.NVarChar).Value = contentType;
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;

            cmd.ExecuteNonQuery();
            
            resp = new NoContentResponse();
        }

        trans.Commit();
    }
    catch
    {
        if(trans != null) trans.Rollback();
        resp = new ServerErrorResponse();
    }
    finally
    {
        conn.Close();
    }

    return resp;
}
 

See Also

IResource Interface | ITHit.WebDAV.Server Namespace