Skip to main content

Overview

The OnlyOffice Control provides flexible methods for uploading and storing documents. All uploaded files are stored in the App_Data/uploads directory with unique identifiers to prevent conflicts.

Upload Methods

There are three main methods for loading documents into the editor, each suited for different scenarios.

SetDocumentFromBytes

Load a document from a byte array. This is the most flexible method and is used internally by other methods.
public void SetDocumentFromBytes(byte[] data, string fileName)
Implementation Details (OnlyOfficeEditor.ascx.cs:93-111):
1
Generate Unique File ID
2
A GUID is generated to ensure unique file storage:
3
var fileId = Guid.NewGuid().ToString("N");
4
Preserve File Extension
5
The original file extension is extracted and preserved:
6
var ext = Path.GetExtension(fileName);
var storedName = fileId + ext;
7
Store in App_Data/uploads
8
The file is saved with the unique name:
9
var uploadsDir = HttpContext.Current.Server.MapPath("~/App_Data/uploads");
Directory.CreateDirectory(uploadsDir);
File.WriteAllBytes(Path.Combine(uploadsDir, storedName), data);
10
Generate URLs
11
Document and callback URLs are created:
12
DocumentUrl = BuildAbsoluteUrl(
    "~/Controls/OnlyOfficeEditor/OnlyOfficeHandler.ashx?action=download&fileId=" + 
    HttpUtility.UrlEncode(fileId));
    
CallbackUrl = BuildAbsoluteUrl(
    "~/Controls/OnlyOfficeEditor/OnlyOfficeHandler.ashx?action=callback&fileId=" + 
    HttpUtility.UrlEncode(fileId));
Example Usage:
protected void btnUpload_Click(object sender, EventArgs e)
{
    if (!fuFile.HasFile)
        return;
    
    // Upload from FileUpload control
    docEditor.SetDocumentFromBytes(fuFile.FileBytes, fuFile.FileName);
    litStatus.Text = "Document uploaded successfully";
}

SetDocumentFromFile

Load a document directly from a server file path. This method reads the file and calls SetDocumentFromBytes.
public void SetDocumentFromFile(string serverFilePath, string displayName = null)
Implementation (OnlyOfficeEditor.ascx.cs:113-119):
if (!File.Exists(serverFilePath)) return;
SetDocumentFromBytes(
    File.ReadAllBytes(serverFilePath),
    displayName ?? Path.GetFileName(serverFilePath));
Example Usage:
protected void LoadTemplate_Click(object sender, EventArgs e)
{
    string templatePath = Server.MapPath("~/Templates/contract-template.docx");
    docEditor.SetDocumentFromFile(templatePath, "Contract Template");
}
Use the optional displayName parameter to show a user-friendly name in the editor while storing the file with a different name.

SetDocumentFromUpload

Load a document that has already been uploaded and stored. This method is useful when you manage file uploads separately.
public void SetDocumentFromUpload(string fileId, string originalName)
Implementation (OnlyOfficeEditor.ascx.cs:121-131):
if (string.IsNullOrWhiteSpace(fileId)) return;

DocumentName = originalName ?? fileId;
DocumentKey = GenerateDocumentKey(fileId);
DocumentUrl = BuildAbsoluteUrl(
    "~/Controls/OnlyOfficeEditor/OnlyOfficeHandler.ashx?action=download&fileId=" + 
    HttpUtility.UrlEncode(fileId));
CallbackUrl = BuildAbsoluteUrl(
    "~/Controls/OnlyOfficeEditor/OnlyOfficeHandler.ashx?action=callback&fileId=" + 
    HttpUtility.UrlEncode(fileId));
Example Usage:
// Load previously uploaded document
string existingFileId = Session["CurrentDocumentId"] as string;
if (!string.IsNullOrEmpty(existingFileId))
{
    docEditor.SetDocumentFromUpload(existingFileId, "Previously Uploaded Document");
}

File Storage Structure

Storage Directory

All files are stored in:
YourApp/
└── App_Data/
    └── uploads/
        ├── a1b2c3d4e5f6.docx
        ├── f6e5d4c3b2a1.xlsx
        └── 9z8y7x6w5v4u.pdf

File Naming Convention

Files are stored using the pattern: {GUID}.{extension}
  • GUID ensures uniqueness
  • Original extension is preserved for type detection
  • Original filename is stored separately in DocumentName property
The App_Data folder is automatically protected by IIS and cannot be accessed directly via HTTP, providing security for your documents.

Document Key Generation

The control generates unique document keys for OnlyOffice (OnlyOfficeEditor.ascx.cs:402-409):
private static string GenerateDocumentKey(string fileId)
{
    if (string.IsNullOrWhiteSpace(fileId))
        return Guid.NewGuid().ToString("N");
    var c = fileId.Replace("-", "");
    if (c.Length < 24) c = c.PadRight(24, '0');
    return c.Substring(0, 20) + "_" + c.Substring(c.Length - 4);
}
The document key must be unique and remain constant for the same document. Changing the key will cause OnlyOffice to treat it as a different document.

File Download Handler

The OnlyOfficeHandler.ashx handles document downloads for the OnlyOffice server (OnlyOfficeHandler.ashx.cs:51-69):
private static void Download(HttpContext context)
{
    var fileId = context.Request["fileId"];
    var uploads = UploadsPath(context);
    var path = ResolveStoredFile(uploads, fileId);
    
    if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
    {
        context.Response.StatusCode = 404;
        context.Response.Write("File not found");
        return;
    }

    var fileName = Path.GetFileName(path);
    context.Response.Clear();
    context.Response.ContentType = "application/octet-stream";
    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
    context.Response.WriteFile(path);
    context.Response.End();
}

File Resolution

Files are resolved using a wildcard pattern (OnlyOfficeHandler.ashx.cs:43-49):
private static string ResolveStoredFile(string uploadsDir, string fileId)
{
    if (string.IsNullOrWhiteSpace(fileId)) return null;
    var matches = Directory.GetFiles(uploadsDir, fileId + ".*");
    if (matches.Length > 0) return matches[0];
    return null;
}

Handling Multiple Uploads

For scenarios with multiple file uploads:
protected void ProcessMultipleFiles()
{
    var uploadedFiles = new List<string>();
    
    foreach (HttpPostedFile file in Request.Files)
    {
        if (file.ContentLength > 0)
        {
            byte[] fileData = new byte[file.ContentLength];
            file.InputStream.Read(fileData, 0, file.ContentLength);
            
            // Store file reference
            var fileId = Guid.NewGuid().ToString("N");
            var ext = Path.GetExtension(file.FileName);
            var uploadsDir = Server.MapPath("~/App_Data/uploads");
            Directory.CreateDirectory(uploadsDir);
            File.WriteAllBytes(Path.Combine(uploadsDir, fileId + ext), fileData);
            
            uploadedFiles.Add(fileId);
        }
    }
    
    // Load the first file in editor
    if (uploadedFiles.Count > 0)
    {
        docEditor.SetDocumentFromUpload(uploadedFiles[0], "Document 1");
    }
}

Storage Considerations

Disk Space Management

The App_Data/uploads folder will grow over time. Implement a cleanup strategy:
  • Delete temporary files after conversion
  • Archive old documents
  • Set up periodic cleanup jobs

Cleanup Example

private void CleanupOldFiles()
{
    var uploadsDir = Server.MapPath("~/App_Data/uploads");
    var files = Directory.GetFiles(uploadsDir);
    var cutoffDate = DateTime.Now.AddDays(-7);
    
    foreach (var file in files)
    {
        var fileInfo = new FileInfo(file);
        if (fileInfo.LastAccessTime < cutoffDate)
        {
            try
            {
                File.Delete(file);
            }
            catch
            {
                // Log error
            }
        }
    }
}

Supported File Types

The control automatically detects document types based on file extensions (OnlyOfficeEditor.ascx.cs:411-427):
File TypeExtensionsDocument Type
Spreadsheet.xls, .xlsx, .ods, .csvcell
Presentation.ppt, .pptx, .odpslide
Document.doc, .docx, .odtword

Next Steps

PDF Conversion

Learn how to convert documents to PDF format

Troubleshooting

Solve common upload and storage issues

Build docs developers (and LLMs) love