SoFunction
Updated on 2025-03-06

Complete example of file upload and download tool class implemented by C# [automatically naming of upload files]

This article describes the file upload and download tool class implemented by C#. Share it for your reference, as follows:

The tool class given here is to use C# language to implement file upload and download functions in the VS2013 environment. When uploading, in order to avoid duplication of file names in the server, the file name on the server is used in the form of "server time + 8-bit random code + file name + file suffix"; the download is performed in the form of WebAPI, and the file saving path can be customized after the download is successful.

The specific source code is as follows:

using System;
using ;
using ;
using ;
using ;
using ;
namespace 
{
  public class FileHelper
  {
    /// <summary>
    /// parse the file name into the upload path of the file    /// </summary>
    /// <param name="fileName">File name</param>    /// <param name="path">File path</param>    /// <returns path>Path of file on server</returns>    public static String transPath(string fileName, string path)
    {
      createDir(path);
      //Please server time + 8-bit random code as part of the file name to ensure that the file name is not duplicated      string nowStr = ("yyyyMMddhhmmssff") + (8);
      // Remove the file name of the suffix      string fileNameStr = (0, ("."));
      // File suffix      String suffix = ((".") + 1);
      if (() != "")
      {
        // If the name is not "", it means that the file exists, otherwise it means that the file does not exist.        path += "\\" + fileNameStr + nowStr + "." + suffix;// Define the upload path      }
      return path;
    }
    /// &lt;summary&gt;
    /// Create a file directory    /// &lt;/summary&gt;
    /// <param name="root">root directory</param>    /// &lt;returns &gt;&lt;/returns&gt;
    private static void createDir(String root)
    {
      // Check the directory      if (!((root)))
      {
        ((root));
      }
    }
    /// &lt;summary&gt;
    /// Download the file according to the path of the file on the server. Here, the file is downloaded in WebAPI. After the download is successful, you can customize the file's saving path.    /// &lt;/summary&gt;
    /// <param name="fileName">File name</param>    /// <param name="path">File path</param>    /// &lt;returns&gt;&lt;/returns&gt;
    public static HttpResponseMessage download(string fileName, string path)
    {
      try
      {
        var stream = new FileStream(path, );
        HttpResponseMessage response = new HttpResponseMessage();
         = new StreamContent(stream);
         = new MediaTypeHeaderValue("application/octet-stream");
         = new ContentDispositionHeaderValue("attachment")
        {
          FileName = fileName
        };
        return response;
      }
      catch
      {
        return new HttpResponseMessage();
      }
    }
  }
}

Controller layer call class

[HttpGet]
public HttpResponseMessage UploadAndDownload()
{
  //Upload the file to the root directory on the server  string root = (@"~/upload");
  string fileName = "Test.docx";
  //Resolve the upload path of the file on the server  string path = (fileName, root);
  //Get the file to be uploaded  var files = ;
  HttpPostedFile file = [0];
  //Save the file  ((path));
  //Download word file  return (fileName, path);
}

View layer:

&lt;a href="http://localhost:60179/api/CeshiController/UploadAndDownload" rel="external nofollow" "> Export </a>

For more information about C# related content, please check out the topic of this site:Summary of common techniques for C# file operation》、《Summary of C# traversal algorithm and skills》、《Summary of thread usage techniques for C# programming》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.