Re: FileUpload
- From: Stefan Braumeister <sbraumi@xxxxxxx>
- Date: Tue, 24 Jul 2007 09:55:46 +0200
Ralf schrieb:
Hallo Stefan,
keine Sorge, das ist nur ein Beispielcode den ich bekommen habe.
Die richtige Verbindung geht natürlich über HTTPS.
Mich interessier eigentlich nur ob man das was im Beispielcode mit der
HttpWebRequest Klasse gemacht wird auch mit WebClient.UploadFile möglich
ist.
In der WebClient Klasse finde ich keine Möglichkeit den CookieContainer
zu setzen
und wie kann ich
string stringDataRFC = boundary + "\n" +
"Content-disposition: form-data; name=\"multiple\"\n\n" + "false\n" +
boundary + "\n" +
"Content-Disposition: form-data; name=\"id\"\n\n" + issueID + "\n" +
boundary + "\n" +
"Content-Disposition: form-data; name=\"Attach\"\n\n" + "Attach\n" +
boundary + "\n";
...
damit realisieren? Ist WebClient.UploadFile für sowas gedacht?
Siehe dazu Franks link. Wenn du Cookie Funktionalität benötigst reicht
der normale WebClient nicht mehr.
.
Gruß
Ralf
"Stefan Braumeister" <sbraumi@xxxxxxx> schrieb im Newsbeitrag
news:f833gq$ifs$1@xxxxxxxxxxxx
Ralf schrieb:
Hallo,
ich hab folgenden Beispielcode bekommen, um einen Datei Upload von .NET
aus durchzuführen. (UploadFileWithPost)
Dieser Code ist recht aufwendig und ich hab versucht das Gleiche mit
UploadFile zu machen, hatte aber keinen Erfolg. (siehe
AddAttachmentToIssue2)
Kann mir jemand sagen, was noch fehlt und ob das überhaupt mit
UploadFile geht?
Gruß
Ralf
private bool UploadFileWithPost( string filename, string issueID )
{
try
{
string loginUrl = "http://localhost:8080/secure/AttachFile.jspa"+ "?id="
+ issueID + "&os_username=" + loginName + "&os_password=" + password;
ich hab jetzt gar nicht weitergelesen, Username und Passwort im Klartext
als HTTP Get - *LOL*. Vor allem das secure im Link
// Create the bytes array for the file to upload.
FileInfo fileInfo = new FileInfo(filename);
if (!fileInfo.Exists)
{
return false;
}
HttpWebRequest webRequestRFC =
(HttpWebRequest)WebRequest.Create(loginUrl);
webRequestRFC.CookieContainer = new CookieContainer();
// Auth with server, and fetch cookie for session ID
System.IO.StreamReader responseReader = new
System.IO.StreamReader(webRequestRFC.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
// Create 2nd request
HttpWebRequest webRequestRFC2 =
(HttpWebRequest)WebRequest.Create("http://localhost:8080/secure/AttachFile.jspa");
// Copy cookie
webRequestRFC2.CookieContainer = webRequestRFC.CookieContainer;
// Generate random boundary, should really be random rather than a fixed
string like this
string boundary = "-----------AaB03x--";
string stringDataRFC =
boundary + "\n" +
"Content-disposition: form-data; name=\"multiple\"\n\n" +
"false\n" +
boundary + "\n" +
"Content-Disposition: form-data; name=\"id\"\n\n" +
issueID + "\n" +
boundary + "\n" +
"Content-Disposition: form-data; name=\"Attach\"\n\n" +
"Attach\n" +
boundary + "\n";
stringDataRFC += "Content-Disposition: form-data; name=\"filename.1\";
filename=\"" + filename + "\"\n" +
"Content-Type: application/octet-stream\n\n";
string strEndTag = "\n" + boundary + "\n";
byte[] dataEndTag = Encoding.UTF8.GetBytes(strEndTag);
byte[] dataRFCbytes = Encoding.UTF8.GetBytes(stringDataRFC);
webRequestRFC2.Method = "POST";
webRequestRFC2.Accept = "image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint,
application/msword, application/x-shockwave-flash, */*";
webRequestRFC2.ContentType = "multipart/form-data; boundary=" +
boundary;
webRequestRFC2.ContentLength = dataRFCbytes.Length + dataEndTag.Length +
fileInfo.Length;
System.IO.Stream newStream = webRequestRFC2.GetRequestStream();
newStream.Write(dataRFCbytes, 0, dataRFCbytes.Length);
// Copy file
System.IO.FileStream fileStream = new System.IO.FileStream(filename,
System.IO.FileMode.Open);
BinaryReader binFileReader = new BinaryReader(fileStream);
System.IO.BinaryWriter binWriter = new
System.IO.BinaryWriter(newStream);
int bufSize = 1024;
byte[] buf = new byte[bufSize];
int read = 0;
int total = 0;
while ((read = binFileReader.Read(buf, 0, bufSize)) != 0)
{
total += read;
binWriter.Write(buf, 0, read);
}
// Copy end of tag
newStream.Write(dataEndTag, 0, dataEndTag.Length);
newStream.Close();
binWriter.Close();
binFileReader.Close();
fileStream.Close();
return true;
}
catch (Exception ex)
{
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
public bool AddAttachmentToIssue2(RemoteIssue issue, string fileName,
string filePath)
{
string url = this.url
+ "/secure/AttachFile.jspa"
+ "?os_username=" + Uri.EscapeDataString(this.username)
+ "&os_password=" + Uri.EscapeDataString(this.password)
+ "&multiple=" + "false"
+ "&filename.1=" + Uri.EscapeDataString(filePath)
+ "&comment=" + ""
+ "&commentLevel=" + Uri.EscapeDataString("group:jira-users")
+ "&id=" + issue.id;
WebClient webClient = new WebClient();
byte[] res = webClient.UploadFile(new Uri(url), filePath);
string html = Encoding.ASCII.GetString(res);
return true;
}
- References:
- FileUpload
- From: Ralf
- Re: FileUpload
- From: Stefan Braumeister
- Re: FileUpload
- From: Ralf
- FileUpload
- Prev by Date: Re: Observer Pattern für Winforms Applikation
- Next by Date: TableLayoutPanel
- Previous by thread: Re: FileUpload
- Next by thread: Lauffähigkeit unter Vista sicherstellen
- Index(es):
Relevant Pages
|