httpwebrequest
을 사용하여 MVC 앱의 일부 추가 양식 데이터가있는 파일을 기존 ASP 사이트에 게시합니다.httpwebrequest 동안 .docx 파일에서이 손상의 원인이 될 수있는 것은 무엇입니까?
파일이 .docx 인 경우 항상 손상된 것으로 도착합니다. 다른 것들은 괜찮아 보이지만, 그 포맷이 더 유연 할 수 있습니다.
부주의 한 텍스트에서 원본 및 손상된 파일을 열면 끝에 손상된 파일에 0000
블록이 없다는 것을 알았습니다. 수동으로이 블록을 바꿀 때 파일이 잘 열립니다.
나는 이런 일이 일으키는 내 .NET 코드를 잘못하고 있어요 뭔가가 있나요? 아니면 문제가 더 밀교입니까?
기존 ASP 코드는 Persist's AspUpload을 사용하여 파일을 수신합니다. 이것은 수신 사이트의 다른 곳에서도 사용되며 이전에는 아무런 문제가 없었습니다. 그래서 나는 그 오류가 거기에 있다고 생각하지 않는다. 게다가, 그것은 단순한 호출 일 뿐이며, 무엇이 잘못 될지를 볼 수 없습니다!
Set File = Upload.Files("fileField")
나는 더이 문제를 디버깅을 시작하는 방법에 대한 손실에 있어요. , 이것은 개정 작업 코드
존되었다 - 해결
public async Task<string> TestFileSend()
{
string result;
var postToUrl = "https://www.mywebsite.com/receive-file.asp";
Dictionary<string, string> extraData = new Dictionary<string, string>();
extraData.Add("colour", "red");
extraData.Add("name", "sandra");
var filePath = "/path-to-file/file.docx";
byte[] fileAsByteArray = File.ReadAllBytes(filePath);
// setup data to send
var dataBoundry = "---------------------------9849436581144108930470211272";
var dataBoundryAsBytes = Encoding.ASCII.GetBytes(Environment.NewLine + "--" + dataBoundry + Environment.NewLine);
var startOfFileData = "--" + dataBoundry + Environment.NewLine +
@"Content-Disposition: form-data; name=""fileField""; filename=""file.docx""" + Environment.NewLine;
startOfFileData += @"Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document" + Environment.NewLine + Environment.NewLine;
var startOfFileDataAsBytes = Encoding.UTF8.GetBytes(startOfFileData);
var endOfRequest = "--" + dataBoundry + "--";
byte[] endOfRequestAsBytes = Encoding.UTF8.GetBytes(endOfRequest);
// perform request
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(postToUrl);
httpWebRequest.ContentType = "multipart/form-data; boundary=" + dataBoundry;
httpWebRequest.Method = "POST";
using (var stream = await httpWebRequest.GetRequestStreamAsync())
{
foreach (KeyValuePair<string, string> item in extraData)
{
var dataItemBytes = DataItemAsBytes(item.Key, item.Value);
stream.Write(dataBoundryAsBytes, 0, dataBoundryAsBytes.Length);
stream.Write(dataItemBytes, 0, dataItemBytes.Length);
}
stream.Write(startOfFileDataAsBytes, 0, startOfFileDataAsBytes.Length);
stream.Write(fileAsByteArray, 0, fileAsByteArray.Length);
stream.Write(endOfRequestAsBytes, 0, endOfRequestAsBytes.Length);
}
try
{
using (WebResponse response = httpWebRequest.GetResponse())
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
using (Stream myData = response.GetResponseStream())
using (var reader = new StreamReader(myData))
{
result = reader.ReadToEnd();
}
}
}
catch (WebException e)
{
result = e.Message;
}
return result;
}
문제 :
이
내가 파일을 게시하려면 사용하고 코드입니다 그의 대답을 강타한다. 나는 그가 파일 스트림을 작성한 직후에 제안한 라인을 추가했는데 이제 문제없이 전송됩니다. 나는 최근 다중/폼 데이터에 대한 재생 및public async Task<string> TestFileSend()
{
string result;
var postToUrl = "https://www.mywebsite.com/receive-file.asp";
Dictionary<string, string> extraData = new Dictionary<string, string>();
extraData.Add("colour", "red");
extraData.Add("name", "sandra");
var filePath = "/path-to-file/file.docx";
byte[] fileAsByteArray = File.ReadAllBytes(filePath);
// setup data to send
var dataBoundry = "---------------------------9849436581144108930470211272";
var dataBoundryAsBytes = Encoding.ASCII.GetBytes(Environment.NewLine + "--" + dataBoundry + Environment.NewLine);
var startOfFileData = "--" + dataBoundry + Environment.NewLine +
@"Content-Disposition: form-data; name=""fileField""; filename=""file.docx""" + Environment.NewLine;
startOfFileData += @"Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document" + Environment.NewLine + Environment.NewLine;
var startOfFileDataAsBytes = Encoding.UTF8.GetBytes(startOfFileData);
var endOfRequest = "--" + dataBoundry + "--";
byte[] endOfRequestAsBytes = Encoding.UTF8.GetBytes(endOfRequest);
// perform request
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(postToUrl);
httpWebRequest.ContentType = "multipart/form-data; boundary=" + dataBoundry;
httpWebRequest.Method = "POST";
using (var stream = await httpWebRequest.GetRequestStreamAsync())
{
foreach (KeyValuePair<string, string> item in extraData)
{
var dataItemBytes = DataItemAsBytes(item.Key, item.Value);
stream.Write(dataBoundryAsBytes, 0, dataBoundryAsBytes.Length);
stream.Write(dataItemBytes, 0, dataItemBytes.Length);
}
stream.Write(startOfFileDataAsBytes, 0, startOfFileDataAsBytes.Length);
stream.Write(fileAsByteArray, 0, fileAsByteArray.Length);
// *** THIS ADDITIONAL LINE IS THE KEY
stream.Write(new byte[] { 45, 45 }, 0, 2);
// ***
stream.Write(endOfRequestAsBytes, 0, endOfRequestAsBytes.Length);
}
try
{
using (WebResponse response = httpWebRequest.GetResponse())
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
using (Stream myData = response.GetResponseStream())
using (var reader = new StreamReader(myData))
{
result = reader.ReadToEnd();
}
}
}
catch (WebException e)
{
result = e.Message;
}
return result;
}
Spot on - 시간을 절약 해주었습니다 - 정말 고마워요 :) –