Search code examples
c#sqlwpfdatabasefilestream

A file downloaded from DB is 'Read-Only' (cannot modify)


I have a following Class:

    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Data { get; set; }

I upload .docx file to server using this methods:

        internal void AddDraft(OpenFileDialog openFileDialog, string description)
    {
        FileInfo fi = new FileInfo(openFileDialog.FileName);
        Draft draft = new Draft();
        draft.Description = description;
        draft.Name = fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length);
        draft.Data = getDataFromDraft(openFileDialog);

        RestRequest request = new RestRequest("api/draft", Method.POST);
        request.RequestFormat = DataFormat.Json;
        request.AddBody(draft);

        restClient.Execute(request);
    }

And convert file to Base64 using this:

        private string getDataFromDraft(OpenFileDialog openFileDialog)
    {
        byte[] bytes = null;

        using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (BinaryReader br = new BinaryReader(new BufferedStream(stream)))
        {
            bytes = br.ReadBytes((Int32)stream.Length);
        }

        var tmp = Convert.ToBase64String(bytes);

        return Convert.ToBase64String(bytes);
    }

On the server side, base64 data is converted to byte[] and saved in a table.

Afterward, I use the following method to download it (in a WPF application)

        internal void SaveDraft(SaveFileDialog saveFileDialog, Draft draft)
    {
        byte[] bytes = Convert.FromBase64String(draft.Data);
        using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        using (BinaryWriter bw = new BinaryWriter(new BufferedStream(fs)))
        {
            bw.Write(bytes);
            bw.Flush();
        }
    }

Here is my problem:
All the downloaded files are read-only, but I'd like to modify and save the downloaded file.

Any suggestions?


Solution

  • Ok I found where was the problem. All time i try to save dowloaded file in C:/. When i save it on another location everything was OK.