0

所以我正在尝试将pdf文件上传到restapi。由于某种原因,我的应用程序无法访问我电脑上的文件。

我用来上传的代码:

public void Upload(string token, string FileName, string FileLocation, string Name, int TypeId, int AddressId, string CompanyName, string StreetNr, string Zip, string City, string CountryCode, string CustomFieldName, string CustomFieldValue)
            {
                var client = new HttpClient();
                client.BaseAddress = _API.baseAddress;
                //upload a new form
                client.DefaultRequestHeaders.Date = DateTime.Now;
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token);

                using (var multiPartContent = new MultipartFormDataContent()) 
                {
                    //get te bytes from a file
                    byte[] pdfData;
                    using (var pdf = new FileStream(@FileLocation, FileMode.Open))//Here i get the error.
                    {
                        pdfData = new byte[pdf.Length];
                        pdf.Read(pdfData, 0, (int)pdf.Length);
                    }
                    var fileContent = new ByteArrayContent(pdfData);
                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        FileName = FileName + ".pdf"
                    };

                    //add the bytes to the multipart message
                    multiPartContent.Add(fileContent);

                    //make a json message
                    var json = new FormRest
                    {
                        Name = Name,
                        TypeId = TypeId,
                        AddressId = AddressId,
                        CompanyName = CompanyName,
                        StreetNr = StreetNr,
                        Zip = Zip,
                        City = City,
                        CountryCode = CountryCode,
                        CustomFields = new List<CustomFieldRest>
                            {
                                new CustomFieldRest {Name = CustomFieldName, Value = CustomFieldValue}
                            }
                    };
                    var Content = new JsonContent(json);

                    //add the json message to the multipart message
                    multiPartContent.Add(Content);

                    var result = client.PostAsync("forms", multiPartContent).Result;
                }

            }
        }

编辑:

它现在似乎适用于本地文件。问题是我还必须从网络共享上传文件。我将如何允许应用程序访问位于域共享上的文件?

4

2 回答 2

1

如果FileLocation是文件所在文件夹的路径,则您的代码应如下所示:

using (var pdf = new FileStream(Path.Combine(FileLocation, FileName + ".pdf"), FileMode.Open))
{
  pdfData = new byte[pdf.Length];
  pdf.Read(pdfData, 0, (int)pdf.Length);
}

我可能会改用File.ReadAllBytes

var pdfData = File.ReadAllBytes(Path.Combine(FileLocation, FileName + ".pdf"));
于 2013-06-27T11:47:35.087 回答
0

发现了问题。我尝试连接的共享被隐藏了,所以我不得不在最后使用 $ ,如下所示:

\192.168.1.5\Testshare$\files

于 2013-07-02T14:37:37.050 回答