所以我正在尝试将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;
}
}
}
编辑:
它现在似乎适用于本地文件。问题是我还必须从网络共享上传文件。我将如何允许应用程序访问位于域共享上的文件?